因为我创建了@Directiveas SelectableDirective,我有点困惑,关于如何将多个值传递给custom指令.我已经搜索了很多,但没有得到适当的解决方案在Angular与Typescript.
这是我的示例代码:
父组件为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) 我试图创建新的组件,但它的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)我正在尝试在异步管道上创建自定义管道,我尝试了很多解决方案,但仍然无法正常工作。这是代码片段。
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) 有人可以指导我以下问题:
对于这两个npm包我都很困惑.
我在这两个场景中有点困惑,我们使用@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)
两种情况都有效,有什么区别吗?谁应该更优选?
我正在尝试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) 我已经安装了最新版本的节点即 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 ×7
javascript ×2
typescript ×2
angular-cli ×1
angular-pipe ×1
angular6 ×1
ngoninit ×1
npm ×1
rxjs ×1