我正在玩角度2(目前版本为alpha 26).ng-for和ng-if例如工作正常.但我确实遇到了问题ng-switch.我只是无法让它工作,即没有打印.似乎整个模板都被忽略了.
这是我的组件的代码,也可以在github上找到:
import {Item} from "js/types/item";
import {Component, View, NgFor, NgIf, NgSwitch} from "angular2/angular2";
@Component({
selector: "item-details",
properties: [
"item"
]
})
@View({
template: `<span>You selected {{item.name}},</span>
<span [ng-switch]="item.name">
<template [ng-switch-when]="'Bill'">
<span> who is often called Billy.</span>
</template>
<template [ng-switch-when]="'Bob'">
<span> who is often called Bobby.</span>
</template>
<template [ng-switch-default]">
<span>who has no nickname.</span>
</template>
</span>
<div *ng-if="item.subItems">
<h2>Sub items:</h2>
<ul>
<li *ng-for="#subItem of item.subItems">{{subItem.name}}</li>
</ul>
</div>`,
directives: [NgFor, NgIf, NgSwitch]
}) …Run Code Online (Sandbox Code Playgroud) 我基本上想要创建一个自定义对话框组件,我可以在我的Angular2应用程序中的任何位置使用它,无论应用程序树中的using组件位于何处.为简单起见,我们称之为SayHello组件.
所以,假设我想要SomeComponent.level3.component来调用SayHello.component中的对话框.
在Angular 1.x中,我会将RootScope注入控制器并以此方式点亮对话框.现在,我了解(或多或少)对于Angular2,你可以在组件树上冒泡事件(使用事件发射器),但是从树上的SomeComponent.level3.component向下到SayHello一直冒泡一个事件似乎很乏味. .零件.
所以我想我会创建一个SayHello服务,我会在任何想要点亮对话框的地方注入.这是我制定的代码草图.
myApp.component.ts
import {SayHelloComponent} from "<<folder>>/sayHello.component";
import {BunchOfComponents} from "<<folder>>/bunchOfComponents";
@Component({
directives: [SayHelloComponent],
selector: "my-app",
templateUrl: `<bunch-of-components>Within this component exists
SomeComponent.level3.component </bunch-of-components>
<say-hello showdialog="{{showDialog}}" message="{{message}}">
</say-hello>`
})
export class myAppComponent {
showDialog = false;
message = "";
constructor(private sayHelloService: SayHelloService) {
this.showDialog = sayHelloService.showDialog;
this.message = sayHelloService.message;
}
}
Run Code Online (Sandbox Code Playgroud)
SayHelloService.ts
import {Injectable} from 'angular2/core';
@Injectable()
export class SayHelloService {
public showDialog: boolean = false;
public message: string ="";
constructor() { …Run Code Online (Sandbox Code Playgroud) 我想在Angular 2中的父指令中实现具有子指令的常见Angular 1.x模式.这是我想要的结构.
<foo>
<bar>A</bar>
<bar>B</bar>
<bar>C</bar>
</foo>
Run Code Online (Sandbox Code Playgroud)
我希望这些Bar组件具有发送到click组件的单击事件.
这是我Foo到目前为止:
@Component({
selector: 'foo',
template: `
<div>
<ng-content></ng-content>
</div>
`
})
export class Foo {
@ContentChildren(Bar) items: QueryList<Bar>;
}
Run Code Online (Sandbox Code Playgroud)
这是我的Foo:
@Component({
selector: 'Bar',
template: `
<div (click)="clickity()">
<ng-content></ng-content>
</div>
`
})
export class Bar {
clickity() {
console.log('Broadcast this to the parent please!');
}
}
Run Code Online (Sandbox Code Playgroud)
Bar如果Foo点击其中一个,我该如何通知?
我没有使用打字稿而是使用ES6和angular2 alpha39动态加载组件.以下代码与我在我的应用程序中的代码类似.我注意到的是angular2不会创建DynamicComponentLoader和ElementRef的实例并注入构造函数.他们是未定义的.
如何使用ES6和angular2 alpha39注入DynamicComponentLoader ?
import {Component, View, Inject, DynamicComponentLoader, ElementRef } from 'angular2/angular2'
@Component({
selector: 'dc',
bindings: [ DynamicComponentLoader ]
})
@View({
template: '<b>Some template</b>'
})
class DynamicComponent {}
@Component({
selector: 'my-app'
})
@View({
template: '<div #container></div>'
})
@Inject(DynamicComponentLoader)
@Inject(ElementRef)
export class App {
constructor(
dynamicComponentLoader,
elementRef
) {
dynamicComponentLoader.loadIntoLocation(DynamicComponent, elementRef, 'container');
}
}
Run Code Online (Sandbox Code Playgroud)