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

查看:113
本文介绍了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);
    });

我需要在应用中使用 maindata getMaindata()这是自举之后。因此,我不能有JavaScript的接近(函数(){})(); bootstrap.js 文件保持一切私人的,所以该函数和放大器;变量是应用程序的其它部分进行访问。

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天全站免登陆