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

时间:2015-09-29 12:37:53

标签: javascript angularjs

我有两个部分,每部分都有很多ng模型。

partial1.html

<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

<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

<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,即我将如何访问和返回这些数据?

1 个答案:

答案 0 :(得分:0)

首先,将您的对象中的所有ng-model更改为:

<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方法中轻松阅读和整理这些数据的价值,执行以下操作:

$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
};