javascript - ng-switch not reacting to dynamic value change -
i new , experimenting angularjs.
that being said, unsure how bind value ng-switch , change between views dynamically changing 'activeview' variable value.
side note: practice switch between views this? views different ways of displaying same data pretty same context , did not see reason create different page 2 views. oh , views contents directives components.
the html:
<body ng-controller="somecontroller vm"> <div ng-switch="{{vm.activeview}}"> <div ng-switch-when="someview"> directive component </div> <div ng-switch-when="anotherview"> directive component </div> <div ng-switch-default> nothing show </div> </div> </body>
the controller:
(function () { 'use strict'; angular .module('app') .controller('somecontroller', somecontroller); function somecontroller() { var vm = this; vm.activeview = ''; function setviewtype(viewtype) { vm.viewtypes = viewtype; } setviewtype('anotherview'); } })();
so happen default switch visible, event after calling setviewtype('anotherview');
thanks in advance!
- you don't need interpolation when using ng-switch ({{vm.activeview}}).
- you aren't assigning vm.activeview value.
your angular.module doesn't have dependency array. if place define module add it.
(function () { 'use strict'; angular.module('app', []) // add dependency array .controller('somecontroller', somecontroller); function somecontroller() { var vm = this; vm.activeview = 'anotherview'; // change function setviewtype(viewtype) { vm.viewtypes = viewtype; } setviewtype('anotherview'); // changes viewtypes prop } })();
check on jsfiddle.
Comments
Post a Comment