[英]AngularJS ng-if inside ng-repeat does not work
Here is my code: 这是我的代码:
<body ng-controller="QueryCntl">
<div>
<h1> Target:{{target}}</h1>
</div>
<div ng-app="myApp" ng-controller="mytableCtrl">
<div ng-include="'header.html'"></div>
<div id="menuList">
<ul class="menuNav">
<li class="menuList_Slide" ng-repeat="x in names">
<div>
<a href="category.html?categoryid={{x.id}}">{{ x.category }}</a>
</div>
<div ng-if="{{target}} == {{x.id}}"> //Display subcategory if true
<ul id="subCategories">
<li>
<a href="category.html?subcategoryid={{x.id}}">Childid should be displayed</a>
</li>
</ul>
</div>
</li>
</ul>
</div>
</div>
<script>
var app = angular.module('myApp', [], function($locationProvider) {
$locationProvider.html5Mode(true);
});
function QueryCntl($scope, $location) {
$scope.target = $location.search()["categoryid"];
}
app.controller('mytableCtrl', function($scope, $http) {
$http.get("api/topCategories")
.success(function(response) {$scope.names = response;});
});
</script>
I want to display only 1 subcategory if {{target}} is equal to {{x.id}}. 如果{{target}}等于{{x.id}},我只想显示1个子类别。 But for now it prints everything... {{target}} gets value from the url where ?categoryid=some_number and x.id is a value from DB.
但是现在它可以打印所有内容... {{target}}从URL获得值,其中?categoryid = some_number,而x.id是来自DB的值。 These values works fine in my code.
这些值在我的代码中工作正常。
I would appreciate any help. 我将不胜感激任何帮助。
EDIT: ng-if="target == x.id" does not help. 编辑:ng-if =“ target == x.id”没有帮助。
ngIf
不需要使用{{}}
-它已经是Angular表达式:
div ng-if="target == x.id"
Problem solver: I inserted 问题解决者:我插入了
<base href="category.html">
in head tags and deleted bad script source. 在head标签中,并删除了错误的脚本源。
You don't need to interpolate properties within the ng-if
directive's conditional as Angular evaluates the complete expression against the scope already. 您无需在
ng-if
指令的条件内插入属性,因为Angular已根据范围评估了完整的表达式。 Try this: 尝试这个:
ng-if="target == x.id"
ngIf directive expects an expression, so you don't need interpolation tags: ngIf指令需要一个表达式,因此您不需要插值标签:
<div ng-if="target == x.id"> //Display subcategory if true
<ul id="subCategories">
<li>
<a href="category.html?subcategoryid={{x.id}}">Childid should be displayed</a>
</li>
</ul>
</div>
And one more thing: you are not initializing $scope.target
because corresponding controller is outside of the ng-app
. 还有一件事:您没有初始化
$scope.target
因为相应的控制器在ng-app
。 Remove ng-controller="QueryCntl"
and add target initialization into mytabeCtrl
: 删除
ng-controller="QueryCntl"
并将目标初始化添加到mytabeCtrl
:
app.controller('mytableCtrl', function($scope, $http) {
$scope.target = $location.search()["categoryid"];
$http.get("api/topCategories")
.success(function(response) {$scope.names = response;});
});
Replace: 更换:
<div ng-if="{{target}} == {{x.id}}">
With: 带有:
<div ng-if="target == x.id">
The ng-if
attribute should contain plain JavaScript, instead of those interpolation tags ( {{}}
). ng-if
属性应包含纯JavaScript,而不是那些插值标签( {{}}
)。
This holds true for all ng-
attributes. 所有
ng-
Attribute都ng-
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.