小编Shr*_*ree的帖子

如何使用TypeScript将多个参数传递给Angular中的@Directives?

因为我创建了@Directiveas SelectableDirective,我有点困惑,关于如何将多个值传递给custom指令.我已经搜索了很多,但没有得到适当的解决方案在AngularTypescript.

这是我的示例代码:

父组件为MCQComponent:

import { Component, OnInit } from '@angular/core';
import { Question } from '../question/question';
import { AppService } from '../app.service/app.service';
import { SelectableDirective } from '../selectable.directive/selectable.directive';
import { ResultComponent } from '../result-component/result.component';

@Component({
    selector: 'mcq-component',
    template: "
         .....
        <div *ngIf = 'isQuestionView'>
            <ul>
                <li *ngFor = 'let opt of currentQuestion.options' 
                    [selectable] = 'opt'
                    (selectedOption) = 'onOptionSelection($event)'>
                    {{opt.option}}
                </li>
            </ul>
            .....
        </div>

    "
    providers: [AppService],
    directives: [SelectableDirective, …
Run Code Online (Sandbox Code Playgroud)

javascript typescript angular angular6

54
推荐指数
3
解决办法
7万
查看次数

为什么ngOnInit打了两次电话?

我试图创建新的组件,但它的ngOnInit()方法被调用两次,我不知道为什么会发生这种情况?这里我创建了一个名为ResultComponent的组件,它从名为mcq-component的父组件中获取@Input. 这是代码:

父组件(MCQComponent)

import { Component, OnInit } from '@angular/core';

@Component({
    selector: 'mcq-component',
    template: `
        <div *ngIf = 'isQuestionView'>
            .....
        </div>
        <result-comp *ngIf = '!isQuestionView' [answers] = 'ansArray'><result-comp>
    `,
    styles: [
        `
            ....
        `
    ],
    providers: [AppService],
    directives: [SelectableDirective, ResultComponent]
})
export class MCQComponent implements OnInit{
      private ansArray:Array<any> = [];
    ....
    constructor(private appService: AppService){}
    ....
}
Run Code Online (Sandbox Code Playgroud)

子组件(result-comp)

import { Component, OnInit, Input } from '@angular/core';

@Component({
    selector:'result-comp',
    template: `
        <h2>Result page:</h2>

    ` …
Run Code Online (Sandbox Code Playgroud)

ngoninit angular

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

如何在异步管道上使用自定义管道?

我正在尝试在异步管道上创建自定义管道,我尝试了很多解决方案,但仍然无法正常工作。这是代码片段。

product.sort.ts - 自定义管道

import { PipeTransform, Pipe } from '@angular/core';
import { Observable } from 'rxjs/Observable';

@Pipe({
    name: 'sortByName'
})
export class ProductPipe implements PipeTransform{
    /*transform(values: Array<any>, term:string){
        return values.filter(obj => obj.pname.startsWith(term))
    }*/

    //CODE NOT WORKING >>>>>
    transform($value: Observable<Array<any>>, term:string){
        if($value){
            $value.subscribe(
                (obj) => {
                    return obj.filter(obj => obj.pname.startsWith(term))
                }
            )
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

products.component.ts - 主要组件

import { Component, OnInit } from '@angular/core';
import { Store } from '@ngrx/store';
import { Observable } from 'rxjs/Observable';
import { AppService …
Run Code Online (Sandbox Code Playgroud)

rxjs angular-pipe angular

7
推荐指数
2
解决办法
1万
查看次数

codelyzer和tslint有什么区别?

有人可以指导我以下问题:

  1. 是什么区别codelyzertslint
  2. 两者都只用于代码分析吗?如果是这样,那么大多数推荐哪一个?
  3. 两者都相互依赖吗?
  4. 我们是否需要同时在同一时间针对任何特定的角度2项目?

对于这两个npm包我都很困惑.

javascript npm typescript angular

7
推荐指数
1
解决办法
3689
查看次数

@Inject与构造函数注入作为Angular 2中的普通参数有什么区别?

我在这两个场景中有点困惑,我们使用@Injectable()装饰器标记一些类,以便它们可用于注入不同的组件.我只是想知道@Inject()构造函数注入之间的区别是正常的.

场景1 - 使用@Inject():

@Component({
    selector: 'main-app',
    template: `
        ....
        {{_service.getName()}}
        ....
    `
})
export class AppComponent{
    constructor(@Inject(AppService) private _service){}
    ....
}
Run Code Online (Sandbox Code Playgroud)

场景2 - 用作Normal参数:

@Component({
    selector: 'main-app',
    template: `
        ....
        {{_service.getName()}}
    `
})
export class AppComponent{
    constructor(private _service:AppService){}
    ....
}
Run Code Online (Sandbox Code Playgroud)

两种情况都有效,有什么区别吗?谁应该更优选?

angular

5
推荐指数
1
解决办法
1839
查看次数

无法在角度4.3中使用HttpInterceptor进行缓存

我正在尝试HttpRequest使用HttpInterceptor角度4.3的文档来实现缓存.但是我收到了一个错误.这是我的代码:

caching.interceptor.ts

import { HttpRequest, HttpResponse, HttpInterceptor, HttpHandler, HttpEvent } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs/Observable';

import 'rxjs/add/operator/do';
import 'rxjs/add/observable/of';

abstract class HttpCache {
  abstract get(req: HttpRequest<any>): HttpResponse<any>|null;
  abstract put(req: HttpRequest<any>, resp: HttpResponse<any>): void;
}

@Injectable()
export class CachingInterceptor implements HttpInterceptor {
    constructor(private cache: HttpCache) {}

    intercept(req: HttpRequest<any>, next: HttpHandler) : Observable<HttpEvent<any>> {
        if(req.method !== 'GET'){
            return next.handle(req);
        }

        const cachedResponse = this.cache.get(req);

        if(cachedResponse){
            return Observable.of(cachedResponse);
        }

        return …
Run Code Online (Sandbox Code Playgroud)

angular

5
推荐指数
1
解决办法
2198
查看次数

如何使用 angular-cli 命令删除前缀“app”并创建文件夹明智的组件/模块?

我已经安装了最新版本的节点即 6.10 和 angular-cli,我正在尝试创建文件夹明智的组件和模块,但无法执行此操作,当我执行以下命令时,它将创建组件/模块,但在“应用程序/'文件夹:

ng generate module app.module/app.module
Run Code Online (Sandbox Code Playgroud)

事件我尝试将.angular-cli.json中的前缀属性修改为空白,如下所示,但仍然无法正常工作:

{
  "$schema": "./node_modules/@angular/cli/lib/config/schema.json",
  "project": {
    "name": "app-cli"
  },
  "apps": [
    {
      "root": "src",
      "outDir": "dist",
      "assets": [
        "assets",
        "favicon.ico"
      ],
      "index": "index.html",
      "main": "main.ts",
      "polyfills": "polyfills.ts",
      "test": "test.ts",
      "tsconfig": "tsconfig.app.json",
      "testTsconfig": "tsconfig.spec.json",
      "prefix": "",
      "styles": [
        "styles.css"
      ],
      "scripts": [],
      "environmentSource": "environments/environment.ts",
      "environments": {
        "dev": "environments/environment.ts",
        "prod": "environments/environment.prod.ts"
      }
    }
  ],
  "e2e": {
    "protractor": {
      "config": "./protractor.conf.js"
    }
  },
  "lint": [
    {
      "project": "src/tsconfig.app.json"
    },
    {
      "project": "src/tsconfig.spec.json"
    },
    { …
Run Code Online (Sandbox Code Playgroud)

angular-cli angular

3
推荐指数
1
解决办法
4770
查看次数