Angular ng-init:“通过服务器端脚本注入数据" [英] Angular ng-init: 'injecting data via server side scripting'

查看:83
本文介绍了Angular ng-init:“通过服务器端脚本注入数据"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于ng-init的Angular文档:

From the Angular docs regarding ng-init:

此指令可被滥用以向其中添加不必要的逻辑 您的模板. ngInit只有少数适​​当的用法,例如 关于别名ngRepeat的特殊属性,如演示中所示 以下;以及通过服务器端脚本注入数据.

This directive can be abused to add unnecessary amounts of logic into your templates. There are only a few appropriate uses of ngInit, such as for aliasing special properties of ngRepeat, as seen in the demo below; and for injecting data via server side scripting.

任何人都可以解释第二个用例:

Could anyone explain the second use case:

...以及用于通过服务器端脚本注入数据.

...and for injecting data via server side scripting.

具体来说,使用此指令将值(例如模型记录ID)从Rails/PHP视图传递到Angular控制器是否合适?

Specifically, would it be appropriate to use this directive to pass a value (e.g. model record id) from a Rails/PHP view to an Angular controller?

其他方法(使用$ location)似乎不必要地复杂.

Other methods (using $location) seem unnecessarily complicated.

推荐答案

绝对.如果您的PHP或Rails应用程序生成模板,并且方便(安全)将数据作为ng-init属性值从该应用程序传递到Angular应用程序,则这是一个有效的用例.

Absolutely. If your PHP or Rails app generates a template, and it is convenient (and secure) to pass data from that app to your Angular app as an ng-init attribute value, that is a valid use case.

如果数据是复杂对象和/或可能会更改(或由客户端应用程序更改),则发出$ http请求会更有用.

It is more useful to make an $http request if the data is a complex object and/or is likely to change (or be changed by the client app).

这篇关于Angular ng-init:“通过服务器端脚本注入数据"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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