标签: angular

是否有必要取消订阅由Http方法创建的可观察对象?

您是否需要取消订阅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)

memory-leaks rxjs angular2-http angular

181
推荐指数
8
解决办法
5万
查看次数

安装 npm 包时无法解决依赖树错误

尝试使用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)

显示以下错误: 在此处输入图片说明

npm angularjs typescript angular angular9

181
推荐指数
16
解决办法
28万
查看次数

Angular 2路由器没有基本href设置

我收到错误,找不到原因.这是错误:

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)

routing angular

177
推荐指数
7
解决办法
15万
查看次数

何时在TypeScript/Angular2中使用Interface和Model

我最近使用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数据并绑定到接口/模型.有时我想要一个数据对象,其他时候我想要持有对象的数组.

我应该使用哪一个?为什么?

typescript angular

177
推荐指数
4
解决办法
10万
查看次数

Angular 2.0路由器无法重新加载浏览器

我使用的是Angular 2.0.0-alpha.30版本.当重定向到不同的路由,然后刷新浏览器,它显示无法GET /路由.

你能帮我弄清楚这个错误发生的原因吗?

angular2-routing angular

176
推荐指数
10
解决办法
10万
查看次数

当Injectable类被实例化时,不会调用ngOnInit

为什么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)

javascript typescript angular

175
推荐指数
4
解决办法
9万
查看次数

Angular CLi生成的"spec.ts"文件是什么?

我是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)

typescript angular

172
推荐指数
3
解决办法
10万
查看次数

Angular 2:无法绑定到'ngModel',因为它不是'input'的已知属性

我正在尝试在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)

forms angular2-forms angular2-template angular

172
推荐指数
5
解决办法
19万
查看次数

*ngIf中的Angular 2 @ViewChild

显示模板中相应元素后获取@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),但它会导致太多无用的通话.

答案(Plunker)

angular2-changedetection angular

172
推荐指数
8
解决办法
7万
查看次数

检查已安装的angular-cli版本?

有没有办法检查我的机器上全局安装的angular-cli的特定版本?我在Windows环境中. npm -vnode -v分别只给我npm和node的版本,我似乎无法找到任何带有ng的命令.

我正在尝试运行我正在进行的项目,并使用npm在旧版本的angular-cli上运行.但是,在安装其他演示项目之后,如果不卸载并重新安装特定版本的angular-cli,我的主项目将不再起作用.

node.js npm angular-cli angular

172
推荐指数
9
解决办法
35万
查看次数