Javascript AngularJS-在指令中切换templateUrl
这是我想切换templateUrl的代码示例。仅当在本地存储或后端中保存数据时刷新页面时,此操作才有效Javascript AngularJS-在指令中切换templateUrl,javascript,angularjs,Javascript,Angularjs,这是我想切换templateUrl的代码示例。仅当在本地存储或后端中保存数据时刷新页面时,此操作才有效 app.directive('myPanel', ['myService', function(myService) { if(myService.isThisTrue()) { return { restrict: 'E', templateUrl: '/views/isTrue.html' }
app.directive('myPanel', ['myService', function(myService) {
if(myService.isThisTrue()) {
return {
restrict: 'E',
templateUrl: '/views/isTrue.html'
}
} else {
return {
restrict: 'E',
templateUrl: '/views/isFalse.html'
}
}
}]);
我还没有找到一个像样的方法把它做得更好。有谁有更好的解决方案吗?
templateUrl
可以是一个返回url字符串的函数
app.directive('myPanel', ['myService',function(myService) {
return {
restrict: 'E',
templateUrl: function() {
return myService.isThisTrue() ? '/views/isTrue.html' : '/views/isFalse.html';
}
}
}
])
templateUrl
可以是返回url字符串的函数
app.directive('myPanel', ['myService',function(myService) {
return {
restrict: 'E',
templateUrl: function() {
return myService.isThisTrue() ? '/views/isTrue.html' : '/views/isFalse.html';
}
}
}
])
我认为这可以通过这个流行的问题来回答-有两个好的方法。我认为这可以通过这个流行的问题来回答-有两个好的方法。