我有一个名为的视图myView
,其中我有两个 div 元素,我想ng-show
根据$scope.states['currentState']
is'A'
或的值有条件地使用它们'B'
。当单击调用控制器上的函数$scope.states['currentState']
的锚标记时,会更改的值。doStuff
myController
我遇到的问题是,当单击锚标记并单击doStuff
函数时,它会在控制台上显示 的值$scope.states['currentState']
已被修改,但并未myView
相应地更新命名视图。
以下是app.js
,myView.html
和index.html
文件的代码。该index.html
文件被用作我使用 Express 和 Node.js 渲染<div ui-view></div>
的文件中的一个。index.ejs
应用程序.js
var app = angular.module("app", ['ui.router']).
config(['$stateProvider', '$urlRouterProvider', '$locationProvider', function($stateProvider, $urlRouterProvider, $locationProvider) {
$locationProvider.html5Mode(true);
$urlRouterProvider.otherwise('/');
$stateProvider
.state('home', {
url: '/',
views: {
'': { templateUrl: 'partials/index.html' },
'myView@home': {
templateUrl: 'partials/myView.html',
controller: 'myController'
},
'myOtherView@home': {
templateUrl: 'partials/myOtherView.html',
controller: 'myController'
}
}
});
}])
app.controller("myController", ['$scope', function($scope){
var states = ['A', 'B'];
$scope.states = states;
$scope.states['currentState'] = states['currentState'] || 'A';
$scope.doStuff = function(toState) {
//doing stuff
$scope.states['currentState'] = toState;
console.log($scope.states['currentState']);
};
} ]);
索引.html
<div class="main">
<div class="container">
<div class="row margin-bottom-40">
<div class="col-md-12 col-sm-12">
<div class="content-page">
<div class="row">
<div ui-view="myView"></div>
<div ui-view="myOtherView"></div>
</div>
</div>
</div>
</div>
</div>
</div>
我的视图.html
<div ng-controller='myController'>
<div ng-show="states['currentState'] == 'A'">
//displaying this div if the currentState is A
<a ng-click="doStuff('B')">Do stuff and show B</a>
</div>
<div ng-show="states['currentState'] == 'B'">
//displaying this div if the currentState is B
</div>
</div>
有人可以帮助我理解为什么没有显示div
with ,即使在调用函数states['currentState'] == 'B'
时我看到值console.log($scope.states['currentState'])
从 'A' 更改为 'B' ?doStuff
myController
编辑: 这是我面临的问题的演示。