有人可以解释Angular Promise
和之间的区别Observable
吗?
每个例子都有助于理解这两种情况.在什么情况下我们可以使用每个案例?
启动Angular应用程序时出现以下错误,即使未显示该组件也是如此.
我必须注释掉,以便我的应用程序正常运行.
zone.js:461 Unhandled Promise rejection: Template parse errors:
Can't bind to 'ngModel' since it isn't a known property of 'input'. ("
<div>
<label>Created:</label>
<input type="text" [ERROR ->][(ngModel)]="test" placeholder="foo" />
</div>
</div>"): InterventionDetails@4:28 ; Zone: <root> ; Task: Promise.then ; Value:
Run Code Online (Sandbox Code Playgroud)
我正在看着英雄的掠夺者,但我没有看到任何区别.
这是组件文件:
import { Component, EventEmitter, Input, OnInit, Output } from '@angular/core';
import { Intervention } from '../../model/intervention';
@Component({
selector: 'intervention-details',
templateUrl: 'app/intervention/details/intervention.details.html',
styleUrls: ['app/intervention/details/intervention.details.css']
})
export class InterventionDetails
{
@Input() intervention: Intervention;
public test : string = "toto"; …
Run Code Online (Sandbox Code Playgroud) Angular ngOnInit
默认提供生命周期钩子.
ngOnInit
如果我们已经有了,为什么要使用constructor
?
我正在写一个Angular应用程序,{{myVal}}
我想要显示一个响应.
我怎么做?如果我只是使用绑定语法,innerHTML
它会编码所有div
字符(当然).
我需要以某种方式将a的内部html绑定{{myVal}}
到变量值.
情况:
请帮忙!我试图在我的Angular2应用程序中制作一个非常简单的表单,但无论它从不工作.
角度版本:
Angular 2.0.0 Rc5
错误:
Can't bind to 'formGroup' since it isn't a known property of 'form'
Run Code Online (Sandbox Code Playgroud)
代码:
风景:
<form [formGroup]="newTaskForm" (submit)="createNewTask()">
<div class="form-group">
<label for="name">Name</label>
<input type="text" name="name" required>
</div>
<button type="submit" class="btn btn-default">Submit</button>
</form>
Run Code Online (Sandbox Code Playgroud)
控制器:
import { Component } from '@angular/core';
import { FormGroup, FormControl, Validators, FormBuilder } from '@angular/forms';
import {FormsModule,ReactiveFormsModule} from '@angular/forms';
import { Task } from './task';
@Component({
selector: 'task-add',
templateUrl: 'app/task-add.component.html'
})
export class TaskAddComponent {
newTaskForm: FormGroup;
constructor(fb: FormBuilder)
{
this.newTaskForm …
Run Code Online (Sandbox Code Playgroud) 我应该何时存储Subscription
实例并unsubscribe()
在NgOnDestroy生命周期中调用,何时可以忽略它们?
保存所有订阅会在组件代码中引入很多混乱.
HTTP客户端指南忽略这样的订阅:
getHeroes() {
this.heroService.getHeroes()
.subscribe(
heroes => this.heroes = heroes,
error => this.errorMessage = <any>error);
}
Run Code Online (Sandbox Code Playgroud)
同时," 航线与导航指南"说:
最终,我们会在其他地方导航.路由器将从DOM中删除此组件并将其销毁.在此之前我们需要自己清理.具体来说,我们必须在Angular破坏组件之前取消订阅.如果不这样做可能会造成内存泄漏.
我们取消订阅我们
Observable
的ngOnDestroy
方法.
private sub: any;
ngOnInit() {
this.sub = this.route.params.subscribe(params => {
let id = +params['id']; // (+) converts string 'id' to a number
this.service.getHero(id).then(hero => this.hero = hero);
});
}
ngOnDestroy() {
this.sub.unsubscribe();
}
Run Code Online (Sandbox Code Playgroud) subscription observable rxjs angular angular-component-life-cycle
我正在研究Angular RxJs模式,我不明白a BehaviorSubject
和a之间的区别Observable
.
根据我的理解,a BehaviorSubject
是一个可以随时间变化的值(可以订阅,订阅者可以接收更新的结果).这似乎是一个完全相同的目的Observable
.
你什么时候使用Observable
vs BehaviorSubject
?使用BehaviorSubject
over Observable
或反之亦然有好处吗?
我想为Angular开始一个简单的hello world应用程序.
当我按照官方快速入门中的说明进行操作时,安装程序在我的项目中创建了32,000个文件.
我认为这是一些错误或者我错过了什么,所以我决定使用angular-cli,但在设置项目后我计算了41,000个文件.
我哪里做错了?我错过了一些非常明显的东西吗?
我正在使用Angular,我希望*ngIf else
在此示例中使用(自版本4起可用):
<div *ngIf="isValid">
content here ...
</div>
<div *ngIf="!isValid">
other content here...
</div>
Run Code Online (Sandbox Code Playgroud)
我怎样才能实现同样的行为ngIf else
?
我有一些元素,我想在某些条件下可见.
在AngularJS中我会写
<div ng-show="myVar">stuff</div>
Run Code Online (Sandbox Code Playgroud)
我怎么能在Angular中做到这一点?
angular ×10
rxjs ×3
typescript ×3
javascript ×2
angular-component-life-cycle ×1
if-statement ×1
input ×1
ngoninit ×1
observable ×1
promise ×1
subscription ×1