首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用控制器访问和修改不同部分的数据?

使用控制器访问和修改不同部分的数据?
EN

Stack Overflow用户
提问于 2015-09-29 20:37:53
回答 1查看 17关注 0票数 0

我有两个部分,每个部分都有许多ng-model。

partial1.html

代码语言:javascript
复制
<input ng-model="A" /> //user enters a
<input ng-model="B" /> //user enters b
<input ng-model="C" /> //user enters c
<button ng-click="SavePartialOne()> Save and go to partial 2 </button>

partial2.html

代码语言:javascript
复制
<input ng-model="P" /> //user enters p
<input ng-model="Q" /> //user enters q
<input ng-model="R" /> //user enters r
<button ng-click="SavePartialTwo()> Save and go to partial 3 which is by now populated from all the data filled in partial1 and 2 </button>

partial3.html

代码语言:javascript
复制
<input ng-model="X" /> //angular binds this to auto-populate data from A input box
<input ng-model="Y" /> //auto-populate data from A and P input box
<input ng-model="Z" /> //auto-populate slightly modified data from A input box ilke a is changed to a+1

我需要做的是在所有的分音中提到注释。在这里帮助我使用app.js,例如,我将如何访问和返回此数据?

EN

回答 1

Stack Overflow用户

发布于 2015-09-29 20:53:46

首先,将所有ng-model更改为在对象中可用,如下所示:

代码语言:javascript
复制
<input ng-model="dataModel.A" />
<input ng-model="dataModel.B" />
<input ng-model="dataModel.C" />
<button ng-click="SavePartialOne()> Save and go to partial 2 </button>

<input ng-model="dataModel.P" />

以此类推,以便我们可以在SavePartialTwo方法中轻松读取和组织这些数据的值,请执行以下操作:

代码语言:javascript
复制
$scope.dataModel = {};

$scope.SavePartialTwo = function() {
    $scope.dataModel.X = $scope.dataModel.A;
    $scope.dataModel.Y = $scope.dataModel.A + ' ' + $scope.dataModel.P;
    $scope.dataModel.Z = $scope.dataModel.Z + 1;

    // Then display your 3rd partial
};
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32844242

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档