【发布时间】:2015-09-07 03:54:16
【问题描述】:
我正在尝试在单击链接时加载 HTML 模板。我做了一个包含templateUrl 的指令,它加载一个HTML 文件。当单击链接时,我正在调用一个函数,该链接将带有我们自定义指令“myCustomer”的 div 附加到 index.html 中已经存在的 div 中。到目前为止我所做的一切如下所示,但它不起作用。
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>
脚本.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);
我的客户.html
<p>DIV CONTENT</p>
这是我正在测试此代码的链接here
【问题讨论】:
-
使用
$compile服务 -
是否将
myCustomer指令用于加载templateUrl? -
是的,就是点击链接时加载url
标签: javascript jquery html angularjs angularjs-directive