我有一个角度4的组件,被称为三次.在模板元数据中,我有一个带有指令的div,带有一些这样的绑定.
@import {gServ} from '../gServ.service';
@Component: ({
selector: 'sr-comp',
template: `<div gDirective [cOptions]="dataChart">`
})
export class SGComponent implements OnInit {
@Input('report') public report: IReportInstance;
cOptions:any;
constructor(private gServ: gServ) {
}
ngOnInit(){
this.cOptions = {};
this.cOptions = this.gServ.objectMerge(this.gServ.defaultOpt, this.report.opt);
//this.report.opt is binded to a component when is instantiated.
//this.gServ.objectMerge is a function that merge the two objects
}
}
Run Code Online (Sandbox Code Playgroud)
this.cOptions改变了组件的每个实例,然后在指令中我有这个:
import { Directive, ElementRef, HostListener, Input, OnInit } from '@angular/core';
@Directive({
selector: '[gDirective]'
})
export class SGDirective implements OnInit {
public …Run Code Online (Sandbox Code Playgroud) 使用 ASP.NET 核心项目(Angular)和 Visual Studio 作为我的 IDE。
有没有一种简单的方法来生成 Angular 组件文件并在 Visual Studio 的应用程序模块中注册新组件?
visual-studio angular-components angular asp.net-core-mvc-2.0
我试图在角度文档中显示一个类似(不完全)的动态组件.
我有一个带有viewContainerRef的动态指令
@Directive({
selector: '[dynamicComponent]'
})
export class DynamicComponentDirective {
constructor(public viewContainerRef: ViewContainerRef) { }
}
Run Code Online (Sandbox Code Playgroud)
摘自组件代码
@ViewChild(DynamicComponentDirective) adHost: DynamicComponentDirective;
..
ngAfterViewInit() {
let componentFactory = null;
console.log(component);
componentFactory = this.componentFactoryResolver.resolveComponentFactory(component);
// this.adHost.viewContainerRef.clear();
const viewContainerRef = this.adHost.viewContainerRef;
viewContainerRef.createComponent(componentFactory);
}
Run Code Online (Sandbox Code Playgroud)
最后<ng-template dynamicComponent></ng-template>在模板中添加
我正在尝试通过实现 MatFormFieldControl、ControlValueAccessor 和 Validator 接口来创建自定义表单控件。
但是,当我提供NG_VALUE_ACCESSOR或NG_VALIDATORS...
@Component({
selector: 'fe-phone-number-input',
templateUrl: './phone-number-input.component.html',
styleUrls: ['./phone-number-input.component.scss'],
providers: [
{
provide: MatFormFieldControl,
useExisting: forwardRef(() => PhoneNumberInputComponent)
},
{
provide: NG_VALUE_ACCESSOR,
useExisting: forwardRef(() => PhoneNumberInputComponent),
multi: true,
},
{
provide: NG_VALIDATORS,
useExisting: forwardRef(() => PhoneNumberInputComponent),
multi: true
}
]
})
export class PhoneNumberInputComponent implements MatFormFieldControl<string>,
ControlValueAccessor, Validator, OnDestroy {
...
}
Run Code Online (Sandbox Code Playgroud)
创建循环依赖:
未捕获的错误:模板解析错误:无法实例化循环依赖!控件
这有效:
@Component({
selector: 'fe-phone-number-input',
templateUrl: './phone-number-input.component.html',
styleUrls: ['./phone-number-input.component.scss'],
providers: [
{
provide: MatFormFieldControl,
useExisting: forwardRef(() => PhoneNumberInputComponent)
} …Run Code Online (Sandbox Code Playgroud) typescript angular-material angular-components angular angular5
我有一个组件单元测试,它没有按照我预期的方式处理来自模拟的承诺拒绝。
我在一个组件上有这个函数,它发送一些数据addUserToOrganisation并处理它返回的 Promise:
public onSubmit() {
this.saveStatus = 'Saving';
this.user = this.prepareSaveUser();
this._userService.addUserToOrganisation(this.user)
.then(() => this._router.navigate(['/profile']))
.catch(error => this.reportError(error));
}
Run Code Online (Sandbox Code Playgroud)
在测试此组件时,我提供了一个模拟,用于UserService监视端点addUserToOrganisation并返回某种 Promise:
mockUserService = jasmine.createSpyObj('mockUserService', ['getOrgId', 'addUserToOrganisation']);
mockUserService.getOrgId.and.returnValue(Promise.resolve('an id'));
mockUserService.addUserToOrganisation.and.returnValue(Promise.resolve());
Run Code Online (Sandbox Code Playgroud)
这对于快乐的路径(解决)来说效果很好 - 我可以测试它this._router.navigate()被调用等等。这是这条快乐路径的通过测试:
it('should navigate to /profile if save is successful', fakeAsync(() => {
fixture.detectChanges();
tick();
fixture.detectChanges();
component.userForm.controls['firstName'].setValue('John');
component.userForm.controls['lastName'].setValue('Doe');
component.userForm.controls['email'].setValue('j.d@gmail.com');
component.onSubmit();
tick();
fixture.detectChanges();
expect(mockRouter.navigate).toHaveBeenCalledWith(['/profile']);
}));
Run Code Online (Sandbox Code Playgroud)
然而,我在测试“悲伤”路径时遇到了麻烦。我改变我的模拟以返回 a Promise.reject,尽管我有.catchin onSubmit,但我收到此错误:
Error: Uncaught (in promise): no
Run Code Online (Sandbox Code Playgroud)
所以这很令人困惑。这是我对这条悲伤之路的测试。请注意,我更改了模拟调用的响应。
it('should show Failed …Run Code Online (Sandbox Code Playgroud) 我正在尝试从另一个组件执行一个函数(这两个组件不是兄弟组件)。我的猜测是,我需要使用@Output 和 eventEmitter来完成此任务,或者创建一个Service并订阅Observable以在所有组件中共享相同的数据(我知道如何传递消息(字符串),但我不知道)知道如何执行函数)。我不太确定从哪里开始。我正在尝试从function2执行function1。谁能帮助我如何让它发挥作用?请提供一个plunker。这就是我的项目的样子:
src
|__app(FOLDER)
|__home(FOLDER)
| |
| |__home.component.ts
| |______function2(){
| What do I need to put in here to execute function1?
| }
|
|__products(FOLDER)
|
|__tools(FOLDER)
|
|____tools.component.ts
|____function1(){
alert("I'm inside function 1!!");
}
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,我有一个包含 function2 的文件home.component.ts和一个包含 function1 的文件tools.component.ts ,那么有什么想法如何从 function2 执行 function1 吗?
我有一个实现的Angular组件ControlValueAccessor,但从writeValue不使用初始值调用该方法ngModel.
模板:
<my-comp [(ngModel)]="$ctrl.activeUser"></my-comp>
该组件通过以下方式降级为AngularJS:
.directive('myComp', downgradeComponent({
component: MyComp,
inputs: [],
outputs: [],
}));
Run Code Online (Sandbox Code Playgroud)
我尝试添加ngModel至inputs和outputs,但它不工作.
我正在构建一个 Angular 应用程序(Angular 4/5/6)并且想在我的组件模板中使用 SVG 精灵。
题:
假设我已经生成了我的 SVG 图标精灵 ( icons.svg),我怎样才能让 Angular 将我的 SVG 图标精灵注入/导入到我的组件模板中?
有没有一种方法可以将我的 SVG 图标精灵注入/导入到我的组件中,而不必使用任何 3rd 方模块/指令并使用 Angular 本身在本地进行?
背景/问题:
正如在讨论这篇文章,icons.svg文件将包含所有定义为SVG图标<symbol>。然后我可以<use>假设icons.svg在 DOM 中注入了 ,在我的 HTML 中呈现选定的图标。
我使用IcoMoon app生成了 SVG 精灵,并将其保存icons.svg到我的 Angular 应用程序中。下面是我的示例 Angular 组件 ( app.component.ts ),我在其中尝试注入/导入icons.svg文件并尝试在我的 HTML 中呈现 SVG 图标。然而,Angular 并没有渲染我的 SVG 图标。我似乎错误地注入了 SVG 图标精灵文件。
更新:
我有一个List用于呈现列表的组件。(嗯,我不知道,但我试图将我的问题提炼成一个易于理解的例子)。
组件的模板List具有一个或多个ListItem允许定义列表项的组件,如下所示:
<app-list>
<app-list-item text='foo'></app-list-item>
<app-list-item text='bar'></app-list-item>
</app-list>
Run Code Online (Sandbox Code Playgroud)
...应呈现为:
- 富
- 酒吧
我还有(假设)一个Reminder使用该List组件的组件。该Reminder组件有一个deadline属性,并且在该截止日期之前要做的事情列表是在组件的模板中定义的,使用ListItem我们之前看到的一个或多个组件:
<app-reminder deadline='Today'>
<app-list-item text='foo'></app-list-item>
<app-list-item text='bar'></app-list-item>
</app-reminder>
Run Code Online (Sandbox Code Playgroud)
这应该呈现为:
请记住在今天之前执行以下操作:
- 富
- 酒吧
该List组件非常简单:
@Component({
selector: 'app-list',
template: `
<ul>
<ng-content></ng-content>
</ul>
`
})
export class ListComponent{
@ContentChildren(ListItemComponent) public readonly items: QueryList<ListItemComponent>;
}
Run Code Online (Sandbox Code Playgroud)
该ListItem组件甚至更简单:
@Component({
selector: 'app-list-item',
template: '<li>{{text}}</li>'
})
export class ListItemComponent {
@Input() public text;
}
Run Code Online (Sandbox Code Playgroud)
最后,该Reminder …
在将此问题标记为“重复”之前,请听我说,因为我被这个问题困了几个小时。我已经解决了现有的问题,但找不到任何解决方案。
我正在学习 Angular,并且已经开始使用 Angular 9+ 和 Angular Material。我试图通过阅读官方页面的文档来实现一个简单的 Angular Material 对话框。
我的代码紧跟示例代码,但我不知道为什么我仍然收到此错误消息:
'mat-dialog-content' is not a known element.
'mat-dialog-actions' is not a known element.
Run Code Online (Sandbox Code Playgroud)
该对话框确实出现了,但看起来好像在对话框模板 html 中根本没有渲染任何 Angular Material 组件/指令。即使我使用<button mat-button>Button</button>,它也会呈现为普通按钮而不是 Angular Material 按钮。对话框模板中没有的所有其他内容都可以正常工作。我不知道我在这里做错了什么,但如果有人能指出我的错误,那就太好了!
app.module.ts:(我正在导入MatDialogModule)
...
import { MatDialogModule } from '@angular/material/dialog';
@NgModule({
declarations: [
...
],
imports: [
...
MatDialogModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
Run Code Online (Sandbox Code Playgroud)
mycomponent.ts:
import { Component, OnInit, ViewChild, Inject } from '@angular/core'; …Run Code Online (Sandbox Code Playgroud) angular ×10
typescript ×3
javascript ×2
angular5 ×1
promise ×1
svg ×1
svg-sprite ×1
unit-testing ×1