javascript - How can I modify my directive's templateURL based on values within its controller? -
i have following angularjs directive , controller.
instead of entering html right directive's template
field, set templateurl
based on values of integers ab
, ac
in associated controller.
if ab + ac >= 10
, want use foo.html
otherwise want use bar.html
. how can that?
myapp.directive('mydirective',function(){ return { restrict:'e', scope: { myobject: '=' }, controller: 'mydirectivectrl', controlleras: 'mydrctrl', bindtocontroller: true, template: 'ab={{mydrctrl.myobject.ab}} ac={{mydrctrl.myobject.ac}}' }; } ); myapp.controller('mydirectivectrl', function($scope){ var self = this; $scope.$watch(function() {return self.myobject}, function (objval) { console.log("watch fired"); console.log('objval.ab = ', objval.ab); console.log('objval.ac = ', objval.ac); },true); });
you use nginclude instead:
myapp.directive('mydirective',function(){ return { restrict:'e', scope: { myobject: '=' }, controller: 'mydirectivectrl', controlleras: 'mydrctrl', bindtocontroller: true, template: '<div ng-include="myobject.currentpath"></div>' }; } ); myapp.controller('mydirectivectrl', function($scope){ var self = this; $scope.$watch(function() {return self.myobject}, function (objval) { objval.currentpath = 'bar.html'; if(objval.ab + objval.ac >= 10){ objval.currentpath = 'foo.html'; } },true); });
Comments
Post a Comment