[英]Angular JS - Load a template html from directive on click of a button
I am trying to load a HTML template when a link is clicked.我试图在单击链接时加载 HTML 模板。 I have made a directive that contains
templateUrl
which loads a HTML file.我制作了一个包含加载 HTML 文件的
templateUrl
的指令。 I am calling a function when a link is clicked that appends a div with our custom directive "myCustomer" to a div already in index.html.我在单击链接时调用一个函数,该链接将带有我们的自定义指令“myCustomer”的 div 附加到 index.html 中已有的 div。 whatever i have done so far is shown below, but it doesn't work.
到目前为止我所做的一切如下所示,但它不起作用。
index.html索引.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脚本.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);
my-customer.html我的客户.html
<p>DIV CONTENT</p>
This is because the angular doesn't bind the directives if you append content like this,这是因为如果您附加这样的内容,角度不会绑定指令,
you need to $compile
service to do this and this will bind the directives against your $scope
您需要
$compile
服务来执行此操作,这会将指令绑定到您的$scope
so controller like,所以控制器喜欢,
.controller('Controller', ['$scope', '$compile', function($scope, $compile) {
$scope.showdiv = function(){
var compiledeHTML = $compile("<div my-Customer></div>")($scope);
$("#d").append(compiledeHTML);
};
}])
OR good to do like this,或者这样做很好,
use ng-if
to create or removing element from html,使用
ng-if
从 html 创建或删除元素,
try this code试试这个代码
Controller ,控制器,
....
$scope.anableCustomerDirective = false;
$scope.showdiv = function(){
$scope.anableCustomerDirective = true;
};
HTML HTML
<body ng-app="docsTemplateUrlDirective">
<div ng-controller="Controller">
<a href="#" ng-click="showdiv()">show</a>
<div id="d">
<div my-Customer ng-if='anableCustomerDirective'></div>
</div>
</div>
</body>
Suggestion建议
if your main intention is to place a html content after the click, then you can use ng-include
here and it will much cleaner and no need of a another directive.如果您的主要目的是在点击后放置一个 html 内容,那么您可以在此处使用
ng-include
并且它会更干净并且不需要另一个指令。
<div id="d">
<div ng-include="templateURL"></div>
</div>
$scope.showdiv = function(){
$scope.templateURL = 'my-customer.html';
};
Seems like directive is only meant to load template
from it.I'd suggest you to use ng-include
directive then似乎指令只是为了从中加载
template
。我建议你使用ng-include
指令然后
Markup标记
<a href="#" ng-click="showDiv=true">show</a>
<div id="d"></div>
<div ng-include="showDiv ? 'my-customer.html': ''">
The most common way is to use $compile
.最常见的方法是使用
$compile
。 More info: Dynamically add directive in AngularJS更多信息: 在 AngularJS 中动态添加指令
Then your controller may look like this:那么您的控制器可能如下所示:
angular.module('docsTemplateUrlDirective', [])
.controller('Controller', ['$scope', '$compile', function($scope, $compile) {
$scope.showdiv = function(){
var el = $compile( "<div my-customer></div>" )( $scope );
$('#d').append( el );
};
}]);
Note that the directive invokation should look like this: <div my-customer>
, not <div my-Customer></div>
as you have in your code.请注意,指令调用应如下所示:
<div my-customer>
,而不是代码中的<div my-Customer></div>
。
when you do it manually with append you actually do not run $apply or $digest so the directive is not run, can you try to do it with ng-show or ng-if ... eg当您使用 append 手动执行此操作时,您实际上并没有运行 $apply 或 $digest 因此该指令不会运行,您可以尝试使用 ng-show 或 ng-if ... 例如
<body ng-app="docsTemplateUrlDirective">
<div ng-controller="Controller">
<a href="#" ng-click="showdiv = true">show</a>
<div my-customer ng-if="showdiv"></div>
<div id="d"></div>
</div>
</body>
this way angular runs the $apply and the directive will be rendered.这样 angular 运行 $apply 并且指令将被呈现。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.