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

access and modify data of different partials using controller?

本文关键字:同部 数据 修改 控制器 访问      更新时间:2023-09-26

我有两个偏导数,每个偏导数都有许多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在这里,即我将如何访问和返回这些数据?

首先,更改所有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
};