Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 模型的属性未在更改时更新_Javascript_Angularjs - Fatal编程技术网

Javascript 模型的属性未在更改时更新

Javascript 模型的属性未在更改时更新,javascript,angularjs,Javascript,Angularjs,我需要将状态消息绑定到变量。如果是空的,我需要隐藏div。如果不是空的,则显示div和message。当我更新$scope.statusMessage.msgin控制器时,{{statusMessage.msg}}没有被更新。如何修复它 <div class="alert alert-info" ng-class="{'alert-danger': statusMessage.status == -1, 'alert-success': statusMessage.status

我需要将状态消息绑定到变量。如果是空的,我需要隐藏div。如果不是空的,则显示div和message。当我更新$scope.statusMessage.msgin控制器时,{{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';
}
更新3

更新4

我用。我的配置是:

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

            },
            'status-buttons@welcome': {
                templateUrl: 'views/prototype/welcome/status-buttons.html',
                controller: 'checkInController'
            }

        }
    })
更新5

很奇怪,但当我从面向对象的表示切换到普通变量时,所有这些都起作用了:

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

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

确保您定义了$scope.statusMessage ie:

在尝试在statusButtonAction函数中设置$scope.statusMessage.msg之前

和使用 ng hide=!statusMessage.msg或ng show=statusMessage.msg 相反 ng hide=statusMessage.msg.length==0


请参见此处的工作演示

我认为问题在于使用“==”而不是“==”。前者执行类型强制,结果有点“不可靠”,尤其是对于字符串/数组。。。至少我使用ng hide=myArray.length==0作为控制的方式,并且总是按照它应该的方式工作

但是,我不确定你在ng课堂上想用…==-1等-至少我看不到statusMessage.status在代码中声明或使用

我发现像这样编辑ui路由器配置可以解决问题

...
check-in@welcome': {
                templateUrl: 'views/prototype/welcome/check-in.html' 
/*,
                controller: 'checkInController' */ // should be removed to work!
...

ng hide=statusMessage.msg==请提供JSFIDLE或plnkr…您能告诉我们如何调用statusButtonAction吗?在链接的示例中,操作参数未定义。这意味着未设置statusMessage.msg。删除开关块并始终设置statusMessage.msg会按预期更新$scope当我在Chrome中调试时,它显示$scope.statusMessage.msg获得新值,但hide指令会一直隐藏它,并且在{{{statusMessage.msg}}@Rcola ng hide=中没有任何更改!statusMessage.msg
$scope.statusMessage = '123';
$scope.statusState = null;
$scope.statusMessage = {};
...
check-in@welcome': {
                templateUrl: 'views/prototype/welcome/check-in.html' 
/*,
                controller: 'checkInController' */ // should be removed to work!
...