简体   繁体   English

AngularJS-在指令中切换templateUrl

[英]AngularJS - switching templateUrl in directive

This is an example of my code where I want to switch a templateUrl. 这是我想在其中切换templateUrl的代码示例。 This works only if you refresh the page while holding the data in localstorage or backend. 仅当在将数据保存在本地存储或后端时刷新页面时,此方法才有效。

app.directive('myPanel', ['myService', function(myService) {
    if(myService.isThisTrue()) {
        return {
            restrict: 'E',
            templateUrl: '/views/isTrue.html'
        }
    } else {
        return {
            restrict: 'E',
            templateUrl: '/views/isFalse.html'
        }
    }
}]);

I did not find a decent way yet to do it better. 我还没有找到更好的方法来做到这一点。 Does anyone have a better solution? 有谁有更好的解决方案?

templateUrl can be a function where you return the url string templateUrl可以是您返回url字符串的函数

app.directive('myPanel', ['myService',function(myService) {
    return {
      restrict: 'E',
      templateUrl: function() {
        return myService.isThisTrue() ? '/views/isTrue.html' : '/views/isFalse.html';
      }
    }
  }
])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM