且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Angular - 在ng-view中追加并执行脚本,然后在其中附加模板内容

更新时间:2023-09-26 12:28:58

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,