我怎么能结合多种型号范围在角? [英] How can I combine multiple scope models in Angular?

查看:149
本文介绍了我怎么能结合多种型号范围在角?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

    $scope.formTitle = '';
    $scope.formDesc = '';
    $scope.fields = [];

但想这些组合成一个 $ scope.theForm 所以我可以有,将很容易地转换成JSON一个对象。

but would like to combine these into one $scope.theForm so I can have one object that would be easily converted into JSON.

什么是做到这一点的最好方法是什么?

What's the best way to do this?

推荐答案

没有什么是从在包装类托管属性阻止你。它实际上是使用适用的目的相同性质的推荐的方法(例如像表单属性)。

Nothing is stopping you from hosting your properties in a wrapping class. It is actually a recommended approach to use for the properties that are similar in purpose (like form properties for example).

// create wrapping class
$scope.theForm = {};

// add properties to the class
$scope.theForm.formTitle = '';
$scope.theForm.formDesc = '';
$scope.theForm.fields = [];

也可以在同一个声明的属性语句的,像这样

$scope.theForm = {
    title: '',
    desc: '',
    fields: []
}

HTML 的,和其他地方一样,你只需访问这些的属性的使用的包装类的名称为preFIX: theForm.formTitle NG-重复=字段theForm.fields

In HTML, and everywhere else, you simply access these properties using the wrapping class name as prefix: theForm.formTitle, ng-repeat="field in theForm.fields", etc.

这篇关于我怎么能结合多种型号范围在角?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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