我试图在单击链接时加载HTML模板。我做了一个包含加载HTML文件的templateUrl
的指令。我点击一个链接时会调用一个函数,该链接将div的自定义指令“myCustomer”附加到已在index.html中的div。不管我迄今为止所做的操作如下所示,但它不起作用。Angular JS - 从点击按钮加载指令模板html
的index.html
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Example - example-example12-production</title>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.4.1/angular.min.js"></script>
<script src="script.js"></script>
</head>
<body ng-app="docsTemplateUrlDirective">
<div ng-controller="Controller">
<a href="#" ng-click="showdiv()">show</a>
<div id="d"></div>
</div>
</body>
</html>
的script.js
(function(angular) {
'use strict';
angular.module('docsTemplateUrlDirective', [])
.controller('Controller', ['$scope', function($scope) {
$scope.showdiv = function(){
$("#d").append("<div my-Customer></div>");
};
}])
.directive('myCustomer', function() {
return {
templateUrl: 'my-customer.html'
};
});
})(window.angular);
我-customer.html
<p>DIV CONTENT</p>
这里是链接我在测试这个代码here
使用'$ compile'服务 –
呢'myCustomer'指令意味着加载'templateUrl'? –
是的,它意味着当链接被点击时加载网址 –