当$ scope。$ watch被调用时,当 templateName 更改或更好地刷新整个指令时,我还需要替换指令模板URL和控制器。
请给出解决方案。
.directive("titleContent", function () {
return {
restrict: 'E',
scope: {
titleInfo: "=",
templateName: "="
},
link: function ($scope, element, attrs, scope) {
$scope.$watch('titleInfo', function (data) {
scope.titleInfo = data;
});
$scope.$watch('templateName', function (data) {
scope.templateName = data;
});
},
templateUrl: 'app/common/title/common.overview.html',
controller: function(scope){
if(scope.templateName === 'OVERVIEW'){
return "CommonOverviewController";
}
else if(scope.templateName === 'contacts'){
console.log("change Controller Detected");
}
},
controllerAs: "titleContentCtrl"
};
});