簡體   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