AngularJS没有更新?

时间:2013-07-15 16:09:56

标签: javascript html5 angularjs

我不确定为什么当它的绑定对象改变时它不会改变:

我的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”,但在调用close函数时不会更新。

1 个答案:

答案 0 :(得分:3)

好的小提琴尝试。首先,你有两个ng-controller指令指向同一个函数。其次我不太了解这里的域名,但我猜这是你需要的。继承人fiddle

<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>