我有以下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