如何使用链接来触发ng开关?
我当前的html:
<ul class="nav nav-list bs-docs-sidenav affix sidenav">
<li><a href="#" ng-click="page='resources'">Resources</a></li>
<li><a href="#" ng-click="page='users'">Users</a></li>
</ul>
<div ng-switch on="page">
<div ng-switch-when="resources">
<h1>resources Div</h1>
</div>
<div ng-switch-when="users">
<h1>Users</h1>
</div>
<div ng-switch-default>
<h1>Default</h1>
</div>
</div>
我的控制器:
function Ctrl($scope) {
$scope.page = 'users';
}
我错过了什么?
我偶然发现了一个类似的问题,并通过在作用域上使用对象而不是属性来解决它,以便值不会被ng-switch指令覆盖。
所询问的问题与此解决方案之间的区别在于,我从ng-switch范围内更改了页面值。
在控制器中声明nav对象
$scope.nav = {
page : 1
}
HTML <div ng-switch="nav.page">
<div ng-switch-when="1">
First page
<a href="#" ng-click="nav.page=2">Resources</a>
</div>
<div ng-switch-when="2">
Second page
<a href="#" ng-click="nav.page=3">Resources</a>
</div>
<div ng-switch-when="2">
Third page
</div>
必须将控制器绑定到顶层元素。这是小提琴
HTML:<div ng-app ng-controller="Ctrl">
<ul class="nav nav-list bs-docs-sidenav affix sidenav">
<li><a href="#" ng-click="page='resources'">Resources</a></li>
<li><a href="#" ng-click="page='users'">Users</a></li>
</ul>
<div ng-switch on="page">
<div ng-switch-when="resources">
<h1>resources Div</h1>
</div>
<div ng-switch-when="users">
<h1>Users</h1>
</div>
<div ng-switch-default>
<h1>Default</h1>
</div>
</div>
</div>