-
Notifications
You must be signed in to change notification settings - Fork 40
你好,请问怎么不通过使用路由,来异步加载HTML及JS,并使它成功编译成ANGULAR局部应用? #7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
你的代码中有一段如下: angular.element(document.body).append(html) 这种方式加入的 html 没有经过 angular 处理,所以正确无法识别里面的 ng directive. 你需要用 var html = '<div ng-model="xxx" />';
var dom = angular.element(document.body);
var parentScope = dom.scope();
var newElem = $compile(html)(parentScope);
dom.append(newElem); 上面的代码不一定能工作,只是提供一种思路和范例。 参考: |
我试过,在JS.JS里边加入 |
有controller也可以的,只要能拿到 |
这是我的第二种写法 https://github.com/CHIheart/CHIanimate/blob/master/async2.rar |
这是我的第三种写法 https://github.com/CHIheart/CHIanimate/blob/master/async3.rar |
$scope.load=function() {
require.async(['./try.css','./try.html','./try.js'],function(css,html,js){
$compile(html)($scope);
angular.element(document.body).append(html)
});
} 你的代码有 2 个问题:
新的范例代码如下: js.js angular.module('AppMain',['AppLogin'])
.controller('CtrlMain',['$scope','$compile',function($scope,$compile){
$scope.load=function() {
require.async(['./try.css','./try.html','./try.js'],function(css,html,js) {
var dom = $compile(html)($scope);
var body = angular.element(document.body);
body.append(dom)
body.append('<style>\n' + css + '\n</style>')
});
}
} try.js define(function(require,exports,module){
var asyncLoader = require('angular-async-loader');
var app = angular.module('AppMain');
asyncLoader.configure(app);
app.controller('CtrlTry', ['$scope','$timeout','LoginInfo' , function ($scope,$timeout,LoginInfo) {
$scope.message="This is the message!";
$scope.login=function(){
$timeout(function(){
$scope.LoginInfo.username="已登录用户";
alert(1)
});
}
}]);
}); |
话说,大侠,怎么把你这插件,改成CMD格式的? 我改了好多地方,最后还是不好使,大侠能来一个完整版的代码么?谢谢啦,这个地方我已经卡住两个月了。 仍然是在$compile()()第二个括号,就是链接到作用域那个地方报错。而且try.js里边的控制器仍然没有执行到。 |
|
大侠能来一个完整版的代码么?谢谢啦,这个地方我已经卡住两个月了。 仍然是在$compile()()第二个括号,就是链接到作用域那个地方报错。而且try.js里边的控制器仍然没有执行到。 |
是因为你在 try.js 里面的 controller 是动态加载的。 虽然可以通过 可以参考我的 demo 代码:https://github.com/subchen/angular-async-loader/tree/master/demo |
可是这是本页异步调用小插件,没有涉及到路由啊?而且我是CMD的,不会用require,用的是sea。既然能通过angular-async-loader+$compile来完成,求你帮我写一个完整版的代码呗,谢谢啦。我卡在这里卡好久了。 |
这是这块代码的压缩包。
https://github.com/CHIheart/CHIanimate/blob/master/async.rar
index.html
js.js
try.css
try.html
try.js
sea2.3.0.js,这里边混入了sea的两个插件sea-text和sea-css
现在的效果就是点击按钮之后,加载的CSS跟HTML都是正常的,JS貌似也是正常的,但就是没办法让加进来的HTML变成有效的ANGULAR局部应用。
The text was updated successfully, but these errors were encountered: