Angularjs 自定义指令模板中的作用域变量

Angularjs scope var in template of custom directive

我有一个自定义指令 hello

myapp.directive('hello', function() {
 return {
link : function (scope, elem, attr) {
    scope.url = attr.url;
    console.log(scope.url);
},
template: '<div ng-include="\'[{url}]\'"></div>'
};
});

我的html代码是

<div url="mypage.html"></div>

我在我的 console.log 中得到值 mypage.html,但无法包含它所说的页面

http://127.0.0.1:5000/[%7Burl%7D]

我认为您需要在将 html 绑定到 ng-include 之前清理它,但下面是您面临的问题的替代解决方案

          app.directive('widget', function(){
            return {
                scope: {
                  url: '@'
                },
                template: '<div ng-include="getTemplateUrl()"></div>',                   
                controller: function($scope) {
                  $scope.getTemplateUrl = function() {
                      console.log($scope.url)
                      return $scope.url
                  }
                }
            }
        })

HTML代码

<div url="mypage.html"></div>