Angular - 在ng-view中追加并执行脚本,然后在其中附加模板内容 [英] Angular - append and execute script inside ng-view before append the template content within it

查看:108
本文介绍了Angular - 在ng-view中追加并执行脚本,然后在其中附加模板内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于脚本无法加载到模板中,由于Angular的jQLite没有编写实现它,我决定在Angular之前添加jQuery库,因为它检查jQuery存在,瞧!,它的工作原理。但是,我在这里问一个问题的事实,意味着有一个'但是',所以,脚本在内容加载之前不会执行。当然,我用路线做了一个小技巧。



在模块的配置部分,我做了这个:



 $ routeProvider 
.when( Business /:Context /:查看?,{
templateUrl: function (url){
return 上下文/ + url.Context + / +(url.View || url.Context)+ .html;
}
});



那么让我们说我们设置路径#/ Business / Test他找到一个名为Test.hmtl的文件在/上下文/测试,对吧呃!假设 Test.html 内容是这样的。



  <   script  >  
function (ng){
console .log(ng)
ng.module(' MyApp')。controller(' TestController' function ($ scope,$ route,$ routeParams,$ location,$ http,$ mdDialog){
$ scope.name = TestController;
$ scope.params = $ routeParams;
$ scope.name = John;
});
})(angular)
< / script >

Hola {{ name}}





最后真正的问题是:为什么会发生这种情况?这就像< script> 在执行之后执行或者我不知道,因为,查看控制台 [ ^ ]:



Angular存在但控制器未及时添加。我做错了吗?这种行为允许吗?有没有人能带我参加这次旅行?



我的尝试:



上面有问题解释。没有什么可说的了。

解决方案

routeProvider
.when( Business /:Context /:View?,{
templateUrl: function (url){
return 上下文/ + url。上下文+ / +(url.View || url.Context)+ .html;
}
});



那么让我们说我们设置路径#/ Business / Test他找到一个名为Test.hmtl的文件在/上下文/测试,对吧呃!假设 Test.html 内容是这样的。



  <   script  >  
function (ng){
console .log(ng)
ng.module(' MyApp')。controller(' TestController' function


scope,


路线,

Since script can't be loaded inside templates, due to Angular's jQLite wasn't written to achieve it, I decided to add jQuery library before Angular since it checks for jQuery existence, and voila!, it works. But, the fact that I'm here asking a question, means that there's a 'but', so, the script doesn't execute before content loads. Of course, I made a little trick with routes.

In module's config section, I made this:

$routeProvider
.when("Business/:Context/:View?", {
    templateUrl: function (url) {
        return "Contexts/" + url.Context + "/" + (url.View || url.Context) + ".html";
    }
});


Then let's say we set the route to "#/Business/Test" he most locate a file called Test.hmtl on "/Contexts/Test", right eh!. Let's say Test.html content is this.

<script>
    (function(ng){
        console.log(ng)
        ng.module('MyApp').controller('TestController', function ($scope, $route, $routeParams, $location, $http, $mdDialog) {
            $scope.name = "TestController";
            $scope.params = $routeParams;
            $scope.name = "John";
        });
    })(angular)
</script>

Hola {{ name }}



And finally the real question: why is this happening? It's like the <script> is executed after or I don't know, because, looking the console[^]:

Angular exists but the controller isn't added in time. Am I doing wrong? It this behavior allowed? Can anyone lead me in this trip?

What I have tried:

Explained above in question. There's nothing more to say.

解决方案

routeProvider .when("Business/:Context/:View?", { templateUrl: function (url) { return "Contexts/" + url.Context + "/" + (url.View || url.Context) + ".html"; } });


Then let's say we set the route to "#/Business/Test" he most locate a file called Test.hmtl on "/Contexts/Test", right eh!. Let's say Test.html content is this.

<script>
    (function(ng){
        console.log(ng)
        ng.module('MyApp').controller('TestController', function (


scope,


route,


这篇关于Angular - 在ng-view中追加并执行脚本,然后在其中附加模板内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆