您是否需要取消订阅Angular 2 http调用以防止内存泄漏?
fetchFilm(index) {
var sub = this._http.get(`http://example.com`)
.map(result => result.json())
.map(json => {
dispatch(this.receiveFilm(json));
})
.subscribe(e=>sub.unsubscribe());
...
Run Code Online (Sandbox Code Playgroud) 尝试使用npm i命令安装 npm 包时,出现以下异常:
我尝试重新安装节点 js 包并将代理设置为关闭使用:
set HTTP_PROXY=
set HTTPS_PROXY=
Run Code Online (Sandbox Code Playgroud)
问题仍然存在。我做错了什么?
更新:
当我运行以下命令时:
npm install --legacy-peer-deps
Run Code Online (Sandbox Code Playgroud)
我收到错误,找不到原因.这是错误:
EXCEPTION: Error during instantiation of LocationStrategy! (RouterOutlet -> Router -> Location -> LocationStrategy).
angular2.dev.js:23514 EXCEPTION: Error during instantiation of LocationStrategy! (RouterOutlet -> Router -> Location -> LocationStrategy).BrowserDomAdapter.logError @ angular2.dev.js:23514BrowserDomAdapter.logGroup @ angular2.dev.js:23525ExceptionHandler.call @ angular2.dev.js:1145(anonymous function) @ angular2.dev.js:14801NgZone._notifyOnError @ angular2.dev.js:5796collection_1.StringMapWrapper.merge.onError @ angular2.dev.js:5700run @ angular2-polyfills.js:141(anonymous function) @ angular2.dev.js:5719zoneBoundFn @ angular2-polyfills.js:111lib$es6$promise$$internal$$tryCatch @ angular2-polyfills.js:1511lib$es6$promise$$internal$$invokeCallback @ angular2-polyfills.js:1523lib$es6$promise$$internal$$publish @ angular2-polyfills.js:1494(anonymous function) @ angular2-polyfills.js:243microtask @ angular2.dev.js:5751run @ angular2-polyfills.js:138(anonymous function) @ angular2.dev.js:5719zoneBoundFn @ angular2-polyfills.js:111lib$es6$promise$asap$$flush @ angular2-polyfills.js:1305
angular2.dev.js:23514 ORIGINAL EXCEPTION: No base href set. Please provide a value for …Run Code Online (Sandbox Code Playgroud) 我最近使用TypeScript观看了Angular 2的教程,但不确定何时使用接口以及何时使用模型来保存数据结构.
界面示例:
export interface IProduct {
ProductNumber: number;
ProductName: string;
ProductDescription: string;
}
Run Code Online (Sandbox Code Playgroud)
型号示例:
export class Product {
constructor(
public ProductNumber: number,
public ProductName: string,
public ProductDescription: string
){}
}
Run Code Online (Sandbox Code Playgroud)
我想从URL加载JSON数据并绑定到接口/模型.有时我想要一个数据对象,其他时候我想要持有对象的数组.
我应该使用哪一个?为什么?
我使用的是Angular 2.0.0-alpha.30版本.当重定向到不同的路由,然后刷新浏览器,它显示无法GET /路由.
你能帮我弄清楚这个错误发生的原因吗?
为什么ngOnInit()在Injectable课程解决后没有打电话?
码
import {Injectable, OnInit} from 'angular2/core';
import { RestApiService, RestRequest } from './rest-api.service';
@Injectable()
export class MovieDbService implements OnInit {
constructor(private _movieDbRest: RestApiService){
window.console.log('FROM constructor()');
}
ngOnInit() {
window.console.log('FROM ngOnInit()');
}
}
Run Code Online (Sandbox Code Playgroud)
控制台输出
FROM constructor()
Run Code Online (Sandbox Code Playgroud) 我是Angular 2(和Angular一般......)的新手,我发现它非常吸引人.我正在使用Angular CLi来生成和提供项目.它似乎运作良好 - 虽然对于我的小学习项目,它产生的比我需要的多 - 但这是可以预料的.
我注意到它spec.ts为项目中的每个Angular元素生成(Component,Service,Pipe等).我一直在搜索,但没有找到这些文件的解释.
这些构建文件在使用时通常是隐藏的tsc吗?我想知道因为我想要更改一个命名不好的Component我创建的名称,并发现该名称也在这些spec.ts文件中被引用.
import {
beforeEach,
beforeEachProviders,
describe,
expect,
it,
inject,
} from '@angular/core/testing';
import { ComponentFixture, TestComponentBuilder } from '@angular/compiler/testing';
import { Component } from '@angular/core';
import { By } from '@angular/platform-browser';
import { PovLevelComponent } from './pov-level.component';
describe('Component: PovLevel', () => {
let builder: TestComponentBuilder;
beforeEachProviders(() => [PovLevelComponent]);
beforeEach(inject([TestComponentBuilder], function (tcb: TestComponentBuilder) {
builder = tcb;
}));
it('should inject the component', inject([PovLevelComponent], …Run Code Online (Sandbox Code Playgroud) 我正在尝试在Angular 2中实现Dynamic Forms.我在动态表单中添加了删除和取消等附加功能.我已按照此文档:https://angular.io/docs/ts/latest/cookbook/dynamic-form.html
我对代码做了一些更改.我在这里得到错误.
我该怎么做这个错误?
你可以找到完整的代码在这里:http://plnkr.co/edit/SL949g1hQQrnRUr1XXqt?p=preview,这是在plunker但不是在我的本地系统的工作.
Html代码:
<div>
<form [formGroup]="form">
<div *ngFor="let question of questions" class="form-row">
<label [attr.for]="question.key">{{question.label}}</label>
<div [ngSwitch]="question.controlType">
<input *ngSwitchCase="'textbox'" [formControlName]="question.key"
[id]="question.key" [type]="question.type" [(ngModel)]="question.value">
<select [id]="question.key" [(ngModel)]="question.value" *ngSwitchCase="'dropdown'" [formControlName]="question.key" >
<option *ngFor="let opt of question.options" [ngValue]="opt.key" >{{opt.value}}</option>
</select>
<input *ngSwitchCase="'checkbox'" [(ngModel)]="question.value"
[id]="question.key" [type]="question.type" (change)="question.value = ck.checked" #ck [ngModelOptions]="{standalone: true}">
</div>
<div class="errorMessage" *ngIf="!form.controls[question.key].valid">{{question.label}} is required</div>
</div>
<div class="form-row">
<button type="submit" [disabled]="!form.valid" (click)="onSubmit()">Save</button>
<button type="button" class="btn btn-default" (click)="cancel()">Cancel</button>
<button type="button" class="btn btn-default" (click)="clear()">Clear</button>
</div> …Run Code Online (Sandbox Code Playgroud) 显示模板中相应元素后获取@ViewChild最优雅的方法是什么?以下是一个例子.也有Plunker可用.
模板:
<div id="layout" *ngIf="display">
<div #contentPlaceholder></div>
</div>
Run Code Online (Sandbox Code Playgroud)
零件:
export class AppComponent {
display = false;
@ViewChild('contentPlaceholder', {read: ViewContainerRef}) viewContainerRef;
show() {
this.display = true;
console.log(this.viewContainerRef); // undefined
setTimeout(()=> {
console.log(this.viewContainerRef); // OK
}, 1);
}
}
Run Code Online (Sandbox Code Playgroud)
我有一个默认隐藏其内容的组件.当有人调用@ViewChild方法时,它变得可见.但是,在角度2变化检测完成之前,我无法参考show().我通常将所有必需的操作包装成viewContainerRef如上所示.有更正确的方法吗?
我知道有一个选项setTimeout(()=>{},1),但它会导致太多无用的通话.
有没有办法检查我的机器上全局安装的angular-cli的特定版本?我在Windows环境中. npm -v和node -v分别只给我npm和node的版本,我似乎无法找到任何带有ng的命令.
我正在尝试运行我正在进行的项目,并使用npm在旧版本的angular-cli上运行.但是,在安装其他演示项目之后,如果不卸载并重新安装特定版本的angular-cli,我的主项目将不再起作用.
angular ×10
typescript ×4
npm ×2
angular-cli ×1
angular9 ×1
angularjs ×1
forms ×1
javascript ×1
memory-leaks ×1
node.js ×1
routing ×1
rxjs ×1