我最近声明学习角度js ..请帮助我.. 我想加载和编译需求角度指令(onclick)..我可以加载指令文件(mydiv.js),但mydiv.js文件没有编制.. 这里是我的代码..根据需求加载和编译角度指令
index.html
<html>
<head>
<script src="jquery.min.js"></script>
<script src="angular.js"></script>
<script type="text/javascript" src="app.js"></script>
</head>
<body>
<div ng-app="app">
<button type="button" onclick="disply_directive()">Click</button>
<my-directive ng-init="init()"></my-directive>
</div>
</body>
</html>
app.js
var app = angular.module('app', []);
var disply_directive = function() {
load_scripts('mydiv.js');
}
var load_scripts = function(url) {
if (url) {
var script = document.querySelector("script[src*='"+url+"']");
if (!script) {
var heads = document.getElementsByTagName("head");
if (heads && heads.length) {
var head = heads[0];
if (head) {
script = document.createElement('script');
script.setAttribute('src', url);
script.setAttribute('type', 'text/javascript');
head.appendChild(script);
}}}
return script;
}};
mydiv.js
app.directive('myDirective', function ($http) {
return {
template: '<h1>Hello World!</h1>',
strict: 'E',
link: function(scope) {
},
controller: function($scope) {
$scope.init = function() {
alert("Hello World!");
console.log("Hello World!");
};
}
};
});
在这里,我不加入index.html中的mydiv.js SRC .. 现在onclick按钮它加载js文件,但不显示..我需要修改我的代码.. 谢谢..
感谢您重播,其实我试图按需加载指令,如果它包含在随后的index.html文件mydev.js会只加载页面加载,我试图避免那一个.. –