AngularJS not updating?

AngularJS not updating?

本文关键字:updating not AngularJS      更新时间:2023-09-26

我不知道为什么当它的绑定对象发生变化时,它没有改变:

我的HTML:

 <div id="account-info" ng-controller="AuthenticateCtrl">
     <h5>Account: </h5>
     {{account}}
 </div>
 <div ng-controller="AuthenticateCtrl">
    <div modal="shouldBeOpen" options="opts">
        <div class="modal-header">
            <h3>Select your account</h3>
        </div>
        <div class="modal-body">
            <div class="account-btn" ng-repeat="item in items" ng-click="close(item)">
                {{item}}
            </div>
        </div>
    </div>
</div>

我的JavaScript:

var AuthenticateCtrl = function ($scope) {
    $scope.account= "";
    $scope.open = function() {
        $scope.shouldBeOpen = true;
    };
    $scope.close = function(item) {
        if (item) {
            $scope.shouldBeOpen = false;
            $scope.account= item;
        }
    };
}

出于某种原因,它总是不显示任何内容,或者如果我设置$scope.account="ANY STRING",它将显示"ANY STRING",但在调用关闭函数时不会更新。

用小提琴试试。首先,有两个ng控制器指令指向同一个函数。其次,我不太了解这里的域,但我猜这正是你所需要的。这是一把小提琴。

<div ng-controller="AuthenticateCtrl">
<div id="account-info">
     <h5>Account: </h5>
     {{account.name}}
 </div>
 <div>
    <div modal="shouldBeOpen" options="opts">
        <div class="modal-header">
            <h3>Select your account</h3>
        </div>
        <div class="modal-body">
            <div class="account-btn" ng-repeat="item in items" ng-click="close(item)">
                {{item.name}}
            </div>
        </div>
    </div>
</div>   
</div>
<script>
var myApp = angular.module('myApp',[]);
var AuthenticateCtrl = function ($scope) {
    $scope.opts = {};
    $scope.account = {};
    $scope.items = [
        {'name':'one'},
        {'name':'two'}
    ];
    $scope.open = function() {
        $scope.shouldBeOpen = true;
    };
    $scope.close = function(item) {
        if (item) {
            $scope.shouldBeOpen = false;
            $scope.account= item;
        }
    };
}
</script>