我是 Angular 的新手,正在尝试编写一些简单的应用程序。我在使用 ngSwitch 按需切换相应指令时遇到问题,如下所示。
html标记:
主 html 文件:directiveOne:
.directive('directiveOne',function(){
return {
restrict:"AE",
replace:true,
template:"<div>this is directive one</div>",
controller:"Dir1Controller",
}
})
.directive('directiveTwo',function(){
return {
restrict:"AE",
template:"<div>this is directive 2</div>,
controller:"Dir2Controller"
}
})
以及在控制器之间共享数据的服务。
.factory('ShareDataFct',function($rootScope){
var srv={};
srv.setValue=function(value){
this.srv.var = value;
$rootScope.$broadcast('varChanged');
}
return srv;
})
最后是指令的 3 个控制器。
.controller('MainController',function($scope,ShareDataFct){
$scope.$watch('varChanged',function(){
$scope.myvar=ShareDataFct.var;
})
})
.controller('Dir1Controller',function($scope){
//nothing to do here yet
})
.controller('Dir2Controller',function($scope){
//nothing to do here yet
})
然后我尝试更改 ShareDataFct var,但视图(ng-switch)不会更改以根据 myvar 更改显示指令。请指教。谢谢你。