繁体   English   中英

如何将自定义数据用于ng-if? (angularJS)

[英]how to use custom data into ng-if ? (angularJS)

我有一个json结构,从那开始,我正在使用控制器和路由创建一个表单。 json是:

[{
    "name": "home",
    "section": "link"
},
{
    "name": "about",
    "section": "link"
},
{
    "name": "Style",
    "section": "link"
},{
    "name": "name",
    "type": "text",
    "section": "home"
},
{
    "name": "age",
    "type": "text",
    "section": "home"
}]

js代码是:

var myApp = angular.module("myApp", ['ngRoute']);

myApp.controller("linkCtrl", function($scope, dataService, $route) {
    dataService.getData('json/link.json', function(data){
      $scope.links = data.data;
      $scope.message = "home";    
    });
});

我的HTML代码是:

<h4>{{message}}</h4>
<div ng-repeat="item in links">
    <div ng-if="item.section == 'home'">
        <div ng-switch="item.type">
            <div ng-switch-when="text">
                <input type="text" id={{item.name}} />
            </div>
            <div ng-switch-when="button">
                <button id={{item.name}}>{{item.name}}</button>
            </div>
            <div ng-switch-when="checkbox">
                <input type="checkbox" id={{item.name}} />
            </div>
        </div>
    </div>

这段代码工作正常。 如果我使用ng-if="item.section == {{message}}"ng-if="item.section == '{{message}}'" ,则它不起作用,但在ng-if之外,如果我放了{{message}},它显示“ home”。

为什么它在ng-if语句中不起作用。 我的代码本身提到了基本语法。 我刚刚发布了函数的主要代码

如果代码可以在ng-if内部运行,那么为我的页面使用一个模板将很容易。

那么,有什么办法可以使它起作用?

采用

ng-if="item.section == message"

代替

ng-if="item.section == {{message}}"

您将必须像ng-if =“ item.section === message”一样使用它

请参阅ng-if是指令,并且已经在范围内。 ng-if原型地从其父级继承范围。 ng-if期望您将给出的表达式作为“ item.section === message”,因此无需进行任何嵌套插值,将对message进行评估,因为它在父级作用域中。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM