[棱角2.4.5]
我尝试过它似乎像EventEmitter一样工作:
来自外部的我的组件:
<split (visibleTransitionEnd)="log($event)"></split>
Run Code Online (Sandbox Code Playgroud)组件内部:
@Output() visibleTransitionEnd: Observable<string>
observer: Observer;
constructor() {
const myObs = new Observable(observer => this.observer = observer);
this.visibleTransitionEnd = myObs
.map(x => '> ' + x + ' <')
.debounceTime(20)
.do(() => console.log('here we are!'));
}
Run Code Online (Sandbox Code Playgroud)然后我可以调用内部组件:
// needed condition because if nobody subscribe 'visibleTransitionEnd' > no observer!
if(this.observer) this.observer.next('test');
Run Code Online (Sandbox Code Playgroud)我喜欢这个,因为我的组件中没有订阅.
但这是一个不好的方法吗?什么是风险/错误?
使用它更好Subject吗?
对于JWT身份验证,我现在发出一个post请求,以使用Http与Observables一起使用的新模块来获取令牌.
我有一个Login显示表单的简单组件:
@Component({
selector: 'my-login',
template: `<form (submit)="submitForm($event)">
<input [(ngModel)]="cred.username" type="text" required autofocus>
<input [(ngModel)]="cred.password" type="password" required>
<button type="submit">Connexion</button>
</form>`
})
export class LoginComponent {
private cred: CredentialsModel = new CredentialsModel();
constructor(public auth: Auth) {}
submitForm(e: MouseEvent) {
e.preventDefault();
this.auth.authentificate(this.cred);
}
}
Run Code Online (Sandbox Code Playgroud)
我有一个Auth提出要求的服务:
@Injectable()
export class Auth {
constructor(public http: Http) {}
public authentificate(credentials: CredentialsModel) {
const headers = new Headers();
headers.append('Content-Type', 'application/json');
this.http.post(config.LOGIN_URL, JSON.stringify(credentials), {headers})
.map(res => res.json())
.subscribe(
data => this._saveJwt(data.id_token),
err …Run Code Online (Sandbox Code Playgroud) 我正在玩角2 alpha 44.
我有一个树模型并使用递归来显示它.每组包含"标准","细分"和其他"组".我们可以在任何级别删除和添加所有这些元素.
当我删除元素然后在同一级别添加其他元素时,有一种奇怪的行为.新订单是错误的,新元素获得了更大的position属性,数组在此属性上排序,但它们出现在元素被删除的位置.
新阵列将记录在控制台中,并以正确的顺序显示.如果使用"SHOW/HIDE"按钮删除并添加所有树,则视图现在的顺序正确.
是否有类似ng1 trackBy的ng2 NgFor?我在源头内找不到它.
[angular2 RC4 + @ angular/forms module]
我有一个使用OnPush变化检测的组件包含FormGroup.此表单包含一个FormControl带async验证器的表单.
验证完成后(不再pending),视图不刷新.只有input模糊事件才能刷新视图.
如果我删除OnPush更改检测,它可以正常工作.
这是一个有角度的错误还是我做错了什么?
[angular2 rc1]
是否有可能有这样的组件:
export class MyComp {
@Output() myEvent = new EventEmitter(false)
ngOnDestroy() {
this.myEvent.emit('ngOnDestroy hook');
}
}
Run Code Online (Sandbox Code Playgroud)
并在父母中抓住它:
<myComp (myEvent)="test($event)"></myComp>
这似乎是不可能的,但我想明白为什么?
我知道我可以使用服务来完成.