加载控制器angular.js问题控制器、加载、问题、angular

2023-09-13 05:20:00 作者:疯人院钻石会员

我试图使用 ui.router 解决注入我的控制器在AngularJS,但我在那里的模板加载第一的问题。我的 app.js 下面的定义是:

I'm trying to inject my controllers in AngularJS using the ui.router resolve, but i'm having issues where the template is loading first. My app.js is defined below:

var app = angular.module('app', ['ui.router']);

app.config(function($stateProvider, $urlRouterProvider) {

    $urlRouterProvider.otherwise('/');

    $stateProvider
        .state('view1', {
            url: "/view1/",
            templateUrl: "view1/view1.html",
            resolve: {'': appendStaticFileToPage("view1/view1.js")}
        })

        .state('view2', {
            url: "/view2/",
            templateUrl: "view2/view2.html",
            resolve: {'': appendStaticFileToPage("view2/view2.js")}
        })

    ;

    function appendStaticFileToPage(file) {
        return function($q){
            var script = document.createElement('script');
            script.setAttribute('src', file);
            document.getElementsByTagName('head')[0].appendChild(script);
        };
    }

});

在一个特定的链接状态用户点击,我注入 JS 文件为它在浏览器中。这部分工作正常。但 templateUrl 定义要使用的模板和加载总是第一。由于它定义了一个 NG-控制器什么情况是,该控制器被加载,但由于它不是在尚未页面,我得到的控制台,控制器couldn错误'T被发现。我怎样才能做到这一点?

When the user clicks on a particular link for a state, I inject the js file for it into the browser. That part works fine. But the templateUrl defines the template to be used and that always loads first. Since it defines an ng-controller what happens is that the controller is being loaded, but since its not in the page yet, I get an error in the console that the controller couldn't be found. How can I achieve this?

我已经创建了我的code的简化版本在这里:

I have created a simplified version of my code here:

http://plnkr.co/edit/X9uFOMIJq5tCn7VezSzT?p=$p$ PVIEW

有没有办法来实现什么,我在这里做什么?

Is there a way to achieve what I am doing here?

修改

我previously曾承诺对我追加code,其中我展示如下:

I previously had promises on my append code, which I'm showing below:

function appendStaticFileToPage(file) {
    return function($q){
        var deferred = $q.defer();

        var script = document.createElement('script');
        script.setAttribute('src', file);
        document.getElementsByTagName('head')[0].appendChild(script).ready(function(){
            deferred.resolve();
        });

        return deferred.promise;
    };
}

当我这样做,没有任何反应。它不会加载模板。它也没有表现出我把控制器的的console.log 语句。

When I do this, nothing happens. It doesn't load the template. It also doesn't show the console.log statements I put in the controllers.

推荐答案

自从与.controller注册它不工作,我可以确认脚本正确加载和执行,我试过了,而不是注入 $ controllerProvider 服务,并通过有注册控制器,和它的作品。

Since registering it with .controller isn't working, and I can confirm that the script is loading properly and executing, I tried instead injecting the $controllerProvider service and registering the controller through there, and it works.

app.config(function($stateProvider, $urlRouterProvider, $controllerProvider) {

    $urlRouterProvider.otherwise('/');

    $stateProvider
        .state('view1', {
            url: "/view1/",
            templateUrl: "view1.html",
            resolve: {'': appendStaticFileToPage("view1.js", "MyViewOneController")}
        })

        .state('view2', {
            url: "/view2/",
            templateUrl: "view2.html",
            resolve: {'': appendStaticFileToPage("view2.js", "MyViewTwoController")}
        })

    ;

    function appendStaticFileToPage(file, controllerName) {
        return function($q){
            var deferred = $q.defer();

            var script = document.createElement('script');
            script.onload = function () {
                $controllerProvider.register(controllerName, window[controllerName])
                deferred.resolve();
            };
            script.onerror = function () {
                deferred.reject();
            };
            script.setAttribute('src', file);
            document.getElementsByTagName('head')[0].appendChild(script);

            return deferred.promise;
        };
    }

});

我不得不同时更新view1.js和view2.js如下:

I had to also update view1.js and view2.js as follows:

'use strict';
window.MyViewOneController = function($scope) {
  console.log("Got to view 1");
};