Angular JS - 单击按钮从指令加载模板 html | 单击按钮从指令加载模板
本文介绍了Angular JS - 单击按钮从指令加载模板 html的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述 我正在尝试在单击链接时加载 HTML 模板.我制作了一个包含加载 HTML 文件的 templateUrl
的指令.我在单击链接时调用一个函数,该函数将带有我们的自定义指令myCustomer"的 div 附加到 index.html 中已有的 div.到目前为止我所做的一切如下所示,但它不起作用.
index.html
<html lang="zh-cn"><头><meta charset="UTF-8"><title>示例 - example-example12-production</title><script src="//ajax.googleapis.com/ajax/libs/angularjs/1.4.1/angular.min.js"></script><script src="script.js"></script>头部><body ng-app="docsTemplateUrlDirective"><div ng-controller="控制器"><a href="#" ng-click="showdiv()">show</a><div id="d"></div>
</html>
脚本.js
(function(angular) {'使用严格';angular.module('docsTemplateUrlDirective', []).controller('Controller', ['$scope', function($scope) {$scope.showdiv = function(){$("#d").append("<div my-Customer></div>");};}]).directive('myCustomer', function() {返回 {templateUrl: 'my-customer.html'};});})(window.angular);
我的客户.html
DIV 内容
这是我正在测试此代码的链接此处
解决方案
这是因为如果您附加这样的内容,angular 不会绑定指令,
你需要 $compile
服务来做这个和这个将针对您的 $scope
绑定指令
像控制器一样,
.controller('Controller', ['$scope', '$compile', function($scope, $compile) {$scope.showdiv = function(){varcompiledeHTML = $compile("<div my-Customer></div>")($scope);$("#d").append(compiledeHTML);};}])
这是演示
OR 这样做很好,
使用 ng-if
从 html 创建或删除元素,
试试这个代码
控制器 、
....$scope.anableCustomerDirective = false;$scope.showdiv = function(){$scope.anableCustomerDirective = true;}; HTML
建议
如果你的主要意图是在点击后放置一个html内容,那么你可以使用ng-包括
在这里,它会更干净,不需要另一个指令.
<div ng-include="templateURL"></div>
$scope.showdiv = 函数(){$scope.templateURL = 'my-customer.html';};
找到DEMO
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.
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);
my-customer.html
<p>DIV CONTENT</p>
Here is the link i was testing this code here
解决方案
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
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
OR good to do like this,
use ng-if
to create or removing element from html,
try this code
Controller ,
....
$scope.anableCustomerDirective = false;
$scope.showdiv = function(){
$scope.anableCustomerDirective = true;
};
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.
<div id="d">
<div ng-include="templateURL"></div>
</div>
$scope.showdiv = function(){
$scope.templateURL = 'my-customer.html';
};
find the DEMO
这篇关于Angular JS - 单击按钮从指令加载模板 html的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
09-02 02:03