Tôi có plunker sau:AngularJS - Tại sao biến phạm vi cô lập chỉ thị của tôi là không xác định?
http://plnkr.co/edit/7YUpQ1tEjnUaX01txFcK?p=preview
Khi tôi chạy này, templateUrl là undefined trong phạm vi. Tại sao?
giả định của tôi ở đây là nó đang cố gắng để tìm thấy một biến có tên template.html trong phạm vi phụ huynh, nhưng không thể, vì vậy nó gán nó vào không xác định. Nếu vậy, làm thế nào để vượt qua điều này trong như một chuỗi thay vì một biến phạm vi?
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 Chỉ thị đã được chuyển đến $ biên dịch [https://docs.angularjs.org/api/ng/service/$compile] –
câu trả lời xuất sắc giải thích sự khác biệt trong từng chi tiết.: http://stackoverflow.com/a/14063373/3123195 –