Javascript Angular JS - 单击按钮从指令加载模板 html
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30967382/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Angular JS - Load a template html from directive on click of a button
提问by Vikram Singh Jadon
I am trying to load a HTML template when a link is clicked. I have made a directive that contains templateUrl
which loads a HTML file. 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. whatever i have done so far is shown below, but it doesn't work.
我试图在单击链接时加载 HTML 模板。我制作了一个包含templateUrl
加载 HTML 文件的指令。我在单击链接时调用一个函数,该链接将带有我们的自定义指令“myCustomer”的 div 附加到 index.html 中已有的 div。到目前为止我所做的一切如下所示,但它不起作用。
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>
Here is the link i was testing this code here
这是我在这里测试此代码的链接
回答by Kalhan.Toress
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);
};
}])
here is the DEMO
这是演示
ORgood 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';
};
find the DEMO
找到演示
回答by Pankaj Parkar
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': ''">
回答by Thomas Weglinski
The most common way is to use $compile
. More info: Dynamically add directive in AngularJS
最常见的方法是使用$compile
. 更多信息:在 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>
您在代码中的样子。
回答by Jony-Y
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 ... e.g
当您使用 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 并且指令将被呈现。