Javascript ng开关对动态值变化无反应
我是新手,正在尝试AngularJS 也就是说,我不确定如何将值绑定到ng开关,并通过更改“activeView”变量值在视图之间动态更改 旁注:在这样的视图之间切换是一种好的做法吗?视图是显示相同数据的不同方式,因此其上下文几乎相同,我不认为有理由为这两个视图创建不同的页面。哦,视图内容实际上是指令组件 html:Javascript ng开关对动态值变化无反应,javascript,angularjs,Javascript,Angularjs,我是新手,正在尝试AngularJS 也就是说,我不确定如何将值绑定到ng开关,并通过更改“activeView”变量值在视图之间动态更改 旁注:在这样的视图之间切换是一种好的做法吗?视图是显示相同数据的不同方式,因此其上下文几乎相同,我不认为有理由为这两个视图创建不同的页面。哦,视图内容实际上是指令组件 html: <body ng-controller="SomeController as vm"> <div ng-switch="{{vm.activeView}}
<body ng-controller="SomeController as vm">
<div ng-switch="{{vm.activeView}}">
<div ng-switch-when="someView">
some directive component
</div>
<div ng-switch-when="anotherView">
another directive component
</div>
<div ng-switch-default>
nothing to show
</div>
</div>
</body>
因此,默认开关是可见的,即调用setViewType('anotherView')后的事件
提前谢谢
(function () {
'use strict';
angular.module('app', []) // add the dependency array
.controller('SomeController', SomeController);
function SomeController() {
var vm = this;
vm.activeView = 'anotherView'; // change this
function setViewType(viewType) {
vm.viewTypes = viewType;
}
setViewType('anotherView'); // this changes the viewTypes prop
}
})();
检查此项。松开{{}您不需要插入
ng switch=“vm.activeView”
谢谢您的建议,但是它没有解决问题:|谢谢你的回复。实际上,角度模块是在其他地方定义的,比如“angular.module('app',[]);”,你也是对的,我没有设置任何默认值,这是因为我希望在单击按钮时更改该值,比如我单击一个按钮,它调用setViewType('someView'))然后,我希望activeView值更改,html根据该动态值在开关中显示视图。@SlashJ-setViewType更新viewTypes属性和activeView属性。我设置了一个硬编码值只是为了方便。这也适用于动态分配。
(function () {
'use strict';
angular.module('app', []) // add the dependency array
.controller('SomeController', SomeController);
function SomeController() {
var vm = this;
vm.activeView = 'anotherView'; // change this
function setViewType(viewType) {
vm.viewTypes = viewType;
}
setViewType('anotherView'); // this changes the viewTypes prop
}
})();