我有一个 html 片段,如果变量 $scope.city 为空 html,我想隐藏它:
<div class="col-lg-12" **ng-hide="{{city === ''}}"**>
<h1>{{forecast.data.city.country}} ,{{forecast.data.city.name}} </h1>
<h2>Forcast for : {{city}}</h2>
</div>
<div class="col-lg-8">
<div class="input-group">
<span class="input-group-addon" id="basic-addon1">City@</span>
<input type="text" **ng-model="city"** class="form-control" placeholder="Username" aria-describedby=" basic-addon1">
</div>
</div>
即使它们被绑定(bind),元素也不会实时隐藏,只有当我进入 $scope.city 变量已经为空的页面时,才会有一个来自 varService 的变量 city (我用它来共享多个 Controller 之间的变量)这里是 Angular 代码:
app.controller('forecastController', ['$scope','varService','$http', function($scope,varService,$http){
$scope.$watch('city', function() {
varService.city = $scope.city;
});
$scope.city = varService.city;
$scope.numOfDays = 2;
$scope.days = 2;
$scope.$watchGroup(['days', 'city'], function() {
$http.get('http://api.openweathermap.org/data/2.5/forecast?q='+$scope.city+'&mode=json&appid=e652a2c384655ea24f5b12d2fb84c60f&cnt='+$scope.days+'&units=metric')
.then(function(data) { $scope.forecast = data; });
});
}]);
那么当我的 $scope.city 发生变化时,如何让 ng-hide 实时工作?谢谢。
最佳答案
而不是
ng-hide="{{city === ''}}"
这样写
ng-hide="city.length===0"
city
已绑定(bind)到 Controller 中的 $scope
,并且 ng-hide/ng-show
需要一个表达式。所以你不需要使用双大括号({{}}
)来评估它的真或假,只需提供如下表达式 "city.length== =0”
。
关于javascript - 实时 Angular JS ng-hide 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36768486/