ng隐藏/ng显示在模型更新后不起作用



我有以下

var model = 
{
 UserInfo :null ,
 PlatformID : 1
}
var myApp = angular.module("myApp", []);
myApp.controller("UCtrl",['$scope','$http','$window', function ($scope, $http,$window) { 
$scope.Info =model ;
$scope.SearchUser = function() {
           $http({
            method:"POST",
            url : '/FindUser',
            data: {UserID : 9999}
        }).success(function(data){
            $scope.Info.UserInfo = data;
        });
    };
});
<div ng-hide="{{Info.UserInfo === null}}" >
</div>

当搜索用户时,Info.user会通过通过$http post更新

$scope.Info.User = data ; 

将数据指定给Info.User对象后,即使存在数据,ng隐藏部分也不会显示。

不要在ng-if中使用{{}}。表达式仍将执行:

<!doctype html>
<html>
<head>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.0/angular.js"></script>
</head>
<body ng-app="Example">
    <div data-ng-controller="Cntrl">
        <div ng-hide="UserInfo === null" >
            User info
        </div>
        <button data-ng-click="toggle()">Toggle</button>
    </div>
    <script>
        var app = angular.module("Example", [])
        .controller("Cntrl", function ($scope) {
            $scope.UserInfo = null;
            $scope.toggle = function () {
                if ($scope.UserInfo === null) {
                    $scope.UserInfo = 'some value';
                } else {
                    $scope.UserInfo = null;
                }
            };
        });
    </script>
</body>
</html>

EDIT:这是另一个更像您的代码:http://jsfiddle.net/kpLe544u/3/

我认为问题是您没有在HTML中声明一个控制器,因此您无法从该分区访问变量Info.UserInfo。

节选自我的小提琴:(http://jsfiddle.net/kpLe544u/2/)

HTML

<div ng-app>
  <div ng-controller="MyCtrl">
      <div ng-show="Info.User">data here</div>
      <div ng-show="Info.Test">this won't show</div>
  </div>
</div>

controller.js

function MyCtrl($scope) {
  $scope.Info = {}; 
  $scope.Info.User = "blah blah";
  $scope.Info.Test = null;
}

最新更新