AngularJS 接收数据后手动引导 [英] AngularJS manually bootstrap after receive data

查看:23
本文介绍了AngularJS 接收数据后手动引导的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从服务器接收数据后手动引导 AngularJS.

I am trying to manually bootstrap AngularJS after receiving data from the server.

bootstrap.js:

bootstrap.js:

var initInjector = angular.injector(['ng']);
var $http = initInjector.get('$http');
var maindata;

function bootstrapAngular() {
    angular.element(document).ready(function() {
        angular.bootstrap(document, ['app']);
    });
}

function getMainData() {
    return maindata;
}

$http
    .get('/data')
    .then(function (response) {
        maindata = response.data;
        bootstrapAngular();
    }, function (error) {
        console.log(error);
    });

我需要在应用程序启动后使用 maindatagetMaindata() .因此,我不能在 bootstrap.js 文件上使用 javascript 更接近 (function(){})(); 来保持所有内容的私密性,以便函数 &应用程序的其他部分可以访问变量.

I need to use the maindata or getMaindata() in the app after it is bootstrapped. Therefore, I cannot have javascript closer (function(){})(); on bootstrap.js file to keep everything private, so that the function & variable is accessible to the other part of the app.

是否可以将所有内容设为私密但仍可供应用的其他部分访问?

Is it possible to make everything private but still accessible to the other part of the app?

推荐答案

您可以将 maindata 公开为一个可注入对象,使其在您的应用中可用:

You could expose maindata as an injectable to make it available in your app:

$http
    .get('/data')
    .then(function (response) {
        maindata = response.data;

        angular.module("app").value("MainData", maindata);

        bootstrapAngular();
    }, function (error) {
        console.log(error);
    });

然后你可以将它注入到控制器或服务中:

Then you could inject it in controllers or services:

.controller("MainCtrl", function($scope, MainData){
   $scope.data = MainData;
});

这篇关于AngularJS 接收数据后手动引导的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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