我有以下plunker:AngularJS - 为什么我的指令隔离范围变量未定义?
http://plnkr.co/edit/7YUpQ1tEjnUaX01txFcK?p=preview
当我运行此,templateUrl的范围是不确定的。为什么?
这里我的假设是,它试图在父范围中找到一个名为template.html的变量,但不能,因此它将它分配给未定义的变量。如果是这样,我如何将它作为字符串而不是范围变量传入?
HTML:
<body ng-app="myApp">
<div ng-controller="TestCtrl">
<test-directive ng-model="testModel"
template-url="template.html">
</test-directive>
</div>
</body>
的.js
var app = angular.module("myApp", []);
app.controller("TestCtrl", function($scope) {
$scope.testModel = {}
});
app.directive("testDirective", function() {
return {
restrict: 'E',
scope: {
model: "=ngModel",
templateUrl: "="
},
template: "<div ng-include='templateUrl'></div>",
link: function (scope, element, attrs) {
console.log(scope.templateUrl); // <-- Shows as undefined
}
}
});
指令API已被移至$ compile。[https://docs.angularjs.org/api/ng/service/$compile] – 2016-03-02 13:00:00
解答详细差异的优秀答案: http://stackoverflow.com/a/14063373/3123195 – 2016-05-03 13:03:01