我有一個JSON結構,從那裏,我創建一個使用控制器和路線的窗體。 JSON的是:如何使用自定義數據進入ng-if? (angularJS)
[{
"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之外 - 如果我把{{message}},它顯示「home」。
爲什麼它不在ng-if語句內工作。基本語法在我的代碼本身中提到。我剛剛發佈了我的函數的主代碼
如果代碼在ng-if中工作,那麼它將很容易爲我的頁面使用一個模板。
那麼,有什麼辦法可以使它工作嗎?
刪除引號和括號。即。 'item.section ==消息' – Johan