角变量初始化 [英] angular variable initialization

查看:228
本文介绍了角变量初始化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有角控制器变量$ scope.abc。

I have a variable in angular controller, $scope.abc.

我有帆作为后端。

$ scope.abc的初始值可以由后端生成页面时来确定。页面被显示给用户之后,$ scope.abc可以或可以不被用户改变。

$scope.abc's initial value can be determined by backend when generating the page. After the page is displayed to user, $scope.abc may or may not be changed by the user.

我可以有后台生成一个完整的静态页面,让角查询为$ scope.abc初值后端。我觉得这是没有必要为初始值可以在该页面的一代确定,并应来作为页面的一部分。

I can have backend to generate a complete static page and let angular query the backend for initial value of $scope.abc. I feel it is not necessary as the initial value can be determined at the page's generation and should come as part of the page.

现在的问题是:正在生成网页时如何初始化$ scope.abc?有什么办法提供数据的角度js文件,类似于res.view(,数据)?

The question is: how to initialize $scope.abc when the webpage is being generated? Is there any way to provide data to angular js file, similar to res.view("",data)?

推荐答案

您真的没有很多选择这里。您可以发出多了一个Ajax请求和加载数据。但是,如果指数在页面加载过程中的数据已经是可用它我会不理想。在这种情况下,你可以其注入其他脚本标签和从那里访问它。

You don't really have many options here. You can issue one more ajax request and load data. However it my be not ideal if during index page loading the data is already available. In this case you can inject it into additional script tag and access it from from there.

<script>
    // Make sure data is JSON encoded.
    window.appConfig = <%= data %>;
</script>
<script src="angular.js"></script>
<script src="app.js"></script>

然后我会在应用程序存储在它的数据设定一个恒定的服务:

And then I would set a constant service in the app to store data in it:

angular.module('app').constant('appConfig', {data: window.appConfig});

这种方式,你可以在更有棱角的方式以后使用它,例如:

This way you could use it later in more Angular way, for example:

angular.module('app').controller(function($scope, appConfig) {
    $scope.config = appConfig.data.userId;
});

如果你所关心的全局变量presence 的AppConfig ,您可以在应用程序运行段,恒时已设置删除它。

And if you are concerned with presence of the global variable appConfig, you can remove it in app run block, when constant is already set.

这篇关于角变量初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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