加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

在Angular2中的打字机 – ng切换

发布时间:2020-12-17 09:17:42 所属栏目:安全 来源:网络整理
导读:我正在玩角色2(目前版本为alpha 26). ng-for和ng-if例如正常工作.但是我确实有ng-switch的问题.我只是不能让它工作,即没有打印.好像整个模板被忽略了. 这是我的组件的代码,也可以在github中找到: import {Item} from "js/types/item";import {Component,Vie
我正在玩角色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,NgSwitch]
})
export class ItemDetailsComponent {
    item:Item;
}

基本上这是一个简单的组件,我注入一个具有name属性的项目. name属性真的有一个值,我可以看到它的行< span>你选择了{{item.name}},< / span>工作正常.

我不知道为什么它不起作用从我的理解,一切都应该是正确的.我与github的角度回购比较,unit tests等

这些是我检查的东西,我相信是的,

> NgSwitch是通过指令导入和注入的
>语法是正确的
>项目确实可用(但也许不在NgSwitch的上下文中)?

为了真正确定,我也尝试过如下模板(切换硬编码的字符串或数字):

<span [ng-switch]="'foo'">
 <template [ng-switch-when]="'foo'">
   <span> who is often called foo.</span>
 </template>
 <template [ng-switch-when]="'bar'">
   <span> who is often called bar.</span>
 </template>
</span>

这也不行,所以它必须是真正的基础,我做错了.我恐怕在互联网上找不到任何例子或代码片段.任何帮助将不胜感激,谢谢提前.

您需要导入NgSwitchWhen和NgSwitchDefault,将它们添加到import语句中

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读