我有一项与第三方服务通信的服务。该服务由应用程序中的多个组件执行。每当服务失败时(“DoSomethingWhenFails”函数),我希望在通用通知组件中收到通知。目前,通用通知组件在 app.component 中引用,并且服务被注入到该组件中。
我想到了类似 eventEmitter 的东西,它会在服务中发出,但是在注入服务时我不熟悉这种模式。最好的方法是什么?看我的代码:
应用程序组件.html:
<notify #messageBox ></notify>
Run Code Online (Sandbox Code Playgroud)
组件:
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.scss']
})
export class AppComponent {
@ViewChild('messageBox') messageBox : notify;
constructor(private someService: SomeService )
Run Code Online (Sandbox Code Playgroud)
通用通知组件:
export class notification
{
ShowNotificationWhenTheServiceFails()
{
DoSomethig();
}
}
Run Code Online (Sandbox Code Playgroud)
服务:
@Injectable({
providedIn: 'root'
})
export class Service{
doSomething(): Observable<any> {
return this.http.get<AA>(URL, options).pipe(
connectToThirdPArtyService();
}),
DoSomethingWhenFails();
);
}
Run Code Online (Sandbox Code Playgroud) 系统偏好:windows server 2012,visual studio 2015,p4v.我的系统昨天启动(可能是Windows更新).从那时起,当我打开visual studio 2015 IDE并尝试连接到我以前用过的现有工作区时,我得到了p4vs错误:"对象引用未设置为对象的实例".我不知道问题是什么.
我用 aurelia-validation 插件创建了一个输入表单,它工作得很好。现在我创建了一个自定义元素来替换输入类型文本框。我正在尝试通过使用“validate”关键字设置 value 属性来验证新的自定义元素值 - 但未验证自定义元素输入值。似乎验证控制器没有绑定到自定义元素。验证控制器的 bindings 数组不包含自定义元素。 也许这与应该触发验证的操作(blur\focus out)有关,所以我添加了 blur 事件的调度,但它仍然不起作用。如前所述 - 当它是一个常规元素时,它工作得很好。 这是相关代码(不需要的代码已删除):自定义元素模板:
<template>
<label>
${title}<input name.bind="fieldName"
title.bind="title" focusout.trigger="focusoutAction()" />
</label>
</template>
Run Code Online (Sandbox Code Playgroud)
自定义元素相关的视图模型代码:
@bindable onFocusout;
...
bind(bindingContext) {
var input = this.element.getElementsByTagName("input")[0];
input.type = this.customType || "text";
input.placeholder = this.placeHolder || "";
//input.value.bind = bindingContext.registration.firstName & validate;
}
...
focusoutAction() {
var customEvent = new CustomEvent("blur");
this.element.dispatchEvent(customEvent);
this.onFocusout();
}
Run Code Online (Sandbox Code Playgroud)
相关容器(父)视图代码:
<form-input name="firstName" id="firstName" title="First Name" bind-
value="registration.firstName & validate" field-name="firstName" on-
focusout.call="validateInput()" />
Run Code Online (Sandbox Code Playgroud)
以及相关的视图模型代码:
ValidationRules
.ensure(r => …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用 Html2 输入 type="file" 和 angular2 http.post 请求上传文件。当请求到达 web api 时,它失败了
Request.Content.IsMimeMultipartContent()
Run Code Online (Sandbox Code Playgroud)
使用Postman提交请求时不会失败(当我不在标头中包含 Content-Type 时,因为邮递员会处理它)。
请参阅我的代码:Html:
<input type="file" (change)="fileChange($event)" placeholder="Upload file" accept=".pdf,.doc,.docx,.dwg,.jpeg,.jpg">
Run Code Online (Sandbox Code Playgroud)
服务功能:
uploadFile(event) {
let fileUploadUrl = this.webApiFileUploadURL;
let fileList: FileList = event.target.files;
if(fileList.length > 0) {
let file: File = fileList[0];
let formData:FormData = new FormData();
formData.append('uploadFile', file, file.name);
let headers = new Headers();
headers.append('Content-Type', 'multipart/form-data');
headers.append('Accept', 'application/json');
let options = new RequestOptions({ headers: headers });
this._http.post(`${this.webApiFileUploadURL}`, formData, options)
.map(res => res.json())
.catch(error => Observable.throw(error)) …Run Code Online (Sandbox Code Playgroud) 我正在尝试做一些应该很简单的事情:我想在 *ngFor 中执行一个函数。该函数返回一个对象。我想在一种“let”语句中设置该对象,这样我就可以在 HTML 中使用它的属性:
<div *ngFor="let productGroup of getproductGroupBySomeVariable(variable)">
<!-- This is where I need to set a variable with the output of
the getProductBySomeProperty function-->
<div *ngLet="'{{getProductBySomeProperty(productGroup.someproperty)}}'
as myVar" class="ui-g">
<!-- Here I want to use and display the properties of the
object created by the function above-->
<span>{{myVar.property1}} </span>
<span>{{myVar.property2}} </span> etc....
</div>
</div>
Run Code Online (Sandbox Code Playgroud) 我使用一个组件作为通过路由到达的常规组件,但我希望它也将它用作模态对话框的“目标”,当使用注入组件时:
export class Component1 implements OnInit {
constructor(private service: <someService>,
public dialogRef: MatDialogRef<Component1>, //These 2 lines are used as
//injection from the opener
@Inject(MAT_DIALOG_DATA) public data: any) {}
Run Code Online (Sandbox Code Playgroud)
这是“开瓶器”的代码:
openComponent1aSModalPage()
{
Const dialogRef = this.dialog.open(Component1, {
width: '70%',
height: '70%',
data: {property: propertyValue}
});
Run Code Online (Sandbox Code Playgroud)
我在激活开启器时工作,但如果我尝试使用常规路由到达相同的组件,我会得到:
错误:未捕获(承诺):NullInjectorError:StaticInjectorError(AppModule)[Component1 -> MatDialogRef]:StaticInjectorError(Platform:core)[Component1-> MatDialogRef]:NullInjectorError:没有 MatDialogRef 的提供者!
如何调整该组件以在两种模式下工作?
dependency-injection modal-dialog typescript angular-material angular
我正在尝试使用FetchXML get request查询动态 CRM系统。 当我使用特定的属性\过滤器时,会出现错误: “无效的 Uri:Uri 方案太长。UriFormatException” 。例如:当尝试使用带有“on-or after”运算符的过滤条件时,引用带有时间戳的日期时间。我越来越:
初始查询很大并且有效,但是当我缩短查询并使用特定属性时,会出现错误。我无法指出问题所在。请参阅我的代码,例如:这不起作用:
<filter>
<condition attribute="scheduledend" operator="le" value="2020-03-16T10:23:30" />
</filter>
Run Code Online (Sandbox Code Playgroud)
这是有效的,但没有时间戳:
<filter>
<condition attribute="scheduledend" operator="on-or-before" value="03/16/2020" />
</filter>
Run Code Online (Sandbox Code Playgroud)
让我强调一下——
<filter>
<condition attribute="scheduledend" operator="le" value="2020-03-16T10:23:30" />
</filter>
Run Code Online (Sandbox Code Playgroud)
如果我删除一些查询属性或过滤器可能会起作用 - 所以这只是一个例子 - 我找不到工作\不工作的模式。这个问题的根本原因可能是什么?
我有一个服务负责使用计时器每 x 秒执行一次 httpClient.get 。我需要这个计时器在服务启动时开始运行,因此计时器是在服务 构造函数中定义的。根据我的理解,订阅应该在计时器作用域中注册,如下所示(如果不需要,我不想更改它,除非它不正确)。
只要后端服务器没有出现错误\异常\错误 500 异常,则所有系统都工作正常。现在,我需要两件事:
public IActionResult getSomeErrorAsTest()
{
try
{
throw new Exception("Serer error");
}
catch(Exception ex)
{
return StatusCode(StatusCodes.Status500InternalServerError, new List<string>());
//throw ex;
}
}
Run Code Online (Sandbox Code Playgroud)
这就是服务(假设每个 get 请求中的数据都会发生变化 - 如果确实如此,则无需实现):
export class MyService
{
MyDataSubject = new Subject<any[]>();
MyDataChanged :Observable>any[]> = this.MyDataSubject.asObservable();
subscribe :Subscription;
constructor(private httpClient : HttpClient)
{
this.subscribe = timer(0, 30000).pipe(
switchMap(()=>
this.getData())).subscribe();
}
getData()
{
return this.httpClient.get<any[]>(<controller …Run Code Online (Sandbox Code Playgroud) 我正在使用 ng-select(来自“@ng-select/ng-select”的 NgSelectModule)?这是我的代码:
<ng-container *ngFor="let validFilter of validFiltersData; let i = index" >
<div *ngIf="validFilter.Values && validFilter.Values.length > 0" class=" filter col-md-4 ">
<ng-select [items]="validFilter.Values" bindLabel="Text" bindValue="ID" (change)= "onFilterValueChage($event, validFilter)" class="input-md"
[(ngModel)]="validFilter.selectedValue"></ng-select>
</div>
</ng-container>
Run Code Online (Sandbox Code Playgroud)
validFiltersData 是一个 FilterData 数组:
export class FilterData
{
Name : string;
FormattedName : string = null;
Values : Filter[];
selectedValue : Filter = null;
...
}
Run Code Online (Sandbox Code Playgroud)
和过滤器:
export class Filter
{
public ID: number;
public Text: string;
....
}
Run Code Online (Sandbox Code Playgroud)
我试图将选定的值作为包含 ID 和文本的对象(过滤器),但我总是只得到 …
我有一个nodejs(express)作为服务器端,一个Angular 6作为客户端。在服务器中,我有中间件功能来进行会话检查。如果会话无效或不存在,我想向客户端发送响应,以便客户端可以对其做出反应。我想到从服务器返回401的响应代码,并在客户端中创建某种listener\route-guard\HttpInterceptor,这样 - 它可以管理客户端中的情况(例如将其重定向到登录页面) 。这是我在服务器中的代码:
router.use('/', (req, res, next) =>
{
if (!req.session.user)
{
res.status(401);
}
else{
next();
}
})
Run Code Online (Sandbox Code Playgroud)
我如何在角度应用程序中捕获/收听此响应?
我正在使用angular5 ng-select组件:https : //github.com/ng-select/ng-select, 并尝试在首次加载容器组件时(以编程方式)设置选定值(在模型)。我没有找到任何相关属性或每个项目的isSelected属性。这是我的代码(已过滤):HTML:
<ng-select [items]="filter.values" bindLabel="text" bindValue="id" class="input-md" [(ngModel)]="filter.selectedValue"></ng-select>
Run Code Online (Sandbox Code Playgroud)
模型:
export class FilterData
{
Name : string;
FormattedName : string;
values : Filter[];
selectedValue : Filter = null;
constructor(filterData : FilterData)
{
this.Name = filterData.Name;
this.values = filterData.values;
this.selectedValue = typeof filterData.selectedValue == "undefined" ? null : filterData.selectedValue;
}
}
export class Filter
{
public id: number;
public text: string;
}
Run Code Online (Sandbox Code Playgroud) angular ×6
angular5 ×1
aurelia ×1
c# ×1
dynamics-crm ×1
fetchxml ×1
html ×1
http-post ×1
let ×1
modal-dialog ×1
ngmodel ×1
p4v ×1
postman ×1
rxjs ×1
rxjs-pipe ×1
selecteditem ×1
timer ×1
typescript ×1