2014-12-04 64 views
1

我需要将状态消息绑定到变量。如果它是空的,我需要隐藏div。如果它不是空的显示div和消息。当我在控制器中更新$scope.statusMessage.msg时,{{statusMessage.msg}}未更新。如何解决它?模型的属性没有得到更新

<div class="alert alert-info" 
    ng-class="{'alert-danger': statusMessage.status == -1, 'alert-success': statusMessage.status == 1, 'alert-warning': statusMessage.status == 0}" 
    role="alert" ng-hide="(statusMessage.msg).length == 0">{{statusMessage.msg}}</div> 

$scope.statusButtonAction = function(action){ 
    $scope.statusMessage.msg = 'Validation Error'; 
} 

UPDATE 1

<form role="form" class="form-inline" id="status-buttons">    
    <button type="button" class="btn btn-warning btn-lg" ng-click="statusButtonAction('Validation Error')">Validation Error</button> 
</form> 

UPDATE 2 声明的:

$scope.statusMessage = { 
    msg: '', 
    status: null 
}; 

UPDATE 3

http://jsbin.com/jaquc/2/

UPDATE 4

我用https://github.com/angular-ui/ui-router。和我的配置是:

prototypeApplication.config(function ($stateProvider, $urlRouterProvider) { 
    $stateProvider.state('welcome', { 
     url: '/', 
     views: { 
      '': { 
       templateUrl: 'views/prototype/welcome.html', 
       controller: 'welcomeController' 
      }, 
      '[email protected]': { 
       templateUrl: 'views/components/header.html' 
      }, 
      '[email protected]': { 
       templateUrl: 'views/prototype/welcome/check-in.html', 
       controller: 'checkInController' 

      }, 
      '[email protected]': { 
       templateUrl: 'views/prototype/welcome/status-buttons.html', 
       controller: 'checkInController' 
      } 

     } 
    }) 

UPDATE 5

很奇怪,但是当我从面向对象表示切换到普通变量的所有作品:

$scope.statusMessage = '123'; 
$scope.statusState = null; 

这是否意味着我必须使用一些技巧让AngularJS读取对象的属性值?

+0

'NG隐藏= “statusMessage.msg == ''”' – 2014-12-04 11:00:07

+0

请提供jsFiddle或plnkr ... – 2014-12-04 11:08:06

+0

你能告诉我们你如何调用statusButtonAction? – 2014-12-04 11:08:17

回答

0

我发现,编辑UI路由器这样的配置可以解决问题

... 
[email protected]': { 
       templateUrl: 'views/prototype/welcome/check-in.html' 
/*, 
       controller: 'checkInController' */ // should be removed to work! 
... 
0

确保您定义$scope.statusMessage即:

$scope.statusMessage = {}; 

您尝试在statusButtonAction功能设置$ scope.statusMessage.msg之前。

,并使用 ng-hide="!statusMessage.msg"ng-show="statusMessage.msg" 代替
ng-hide="(statusMessage.msg).length == 0"

请参阅工作演示这里http://jsbin.com/vavahi/1/edit

+0

当我在Chrome中进行调试时,它显示$ scope.statusMessage.msg获得新值,但是'hide'指令保持隐藏并且没有任何变化'{{statusMessage.msg}}' – 2014-12-04 11:36:37

+0

@Rcola ng-hide =“!statusMessage.msg” – sylwester 2014-12-04 11:50:05

+0

@RCola请参阅http://jsbin.com/jaquc/2/edit?html,js,output – sylwester 2014-12-04 11:52:44

0

我认为这个问题是使用 '==',而不是 '==='。前者的类型是强制转换,结果有点“不可靠”,特别是对于字符串/数组...至少我使用ng-hide =“myArray.length === 0”作为控制和工作的方式。 (但是,我不确定你想要在你的ng-class中用“... == -1”来做什么等等 - 至少我没有看到statusMessage.status被声明或者用于你的代码...)