javascript - 实时 Angular JS ng-hide 不起作用

标签 javascript angularjs

我有一个 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/

相关文章:

angularjs - 如何在 Azure 网站中拥有测试和生产 AngularJS 应用程序?

javascript - Angular 模型未在动画完整回调上更新

javascript - 如何在extjs中使用href.replace

javascript - 将变量从 Javascript 发送到文本框

javascript - 使用jquery旋转数组元素

javascript - 我应该在指令和服务之间传递数据吗?

angularjs - ng-disabled with bootstrap 下拉列表

angularjs - 为什么我们在没有延迟参数的情况下调用 $timeout。

javascript - 我可以在 javascript 函数中使用 jquery 方法、选择器吗?

javascript - 按下后按钮保持禁用状态