Javascript AngularJS:对象的推送

Javascript AngularJS:对象的推送,javascript,angularjs,scope,angularjs-scope,push,Javascript,Angularjs,Scope,Angularjs Scope,Push,在我的小应用程序中,我正在读取一个json文件,显示它,并给用户一个向其中添加记录的机会。在特定情况下,condition是patient中的一个数组,我想向数组中添加一个项目(取决于用户输入)。无需存储数据或通过POST发回数据 我不知道我是需要从对象患者还是从$scope获取信息。未提交任何内容。 函数addCondition是否正确 /* Data */ angular.module('prototypeApp') .factory('MedicalRecords', function(

在我的小应用程序中,我正在读取一个json文件,显示它,并给用户一个向其中添加记录的机会。在特定情况下,
condition
patient
中的一个数组,我想向数组中添加一个项目(取决于用户输入)。无需存储数据或通过POST发回数据

我不知道我是需要从对象
患者
还是从$scope获取信息。未提交任何内容。

函数addCondition
是否正确

/* Data */
angular.module('prototypeApp')
 .factory('MedicalRecords', function($http) {
   return $http.get('scripts/data/patient_record.json');
});

/* Add more Information to Patients Conditions */
angular.module('prototypeApp')
 .controller('PatientExtConditionCtrl', function ($scope, MedicalRecords) {
        MedicalRecords.success(function(data) {
        $scope.patient = data;
          });
 });

/* Confirm Patients Conditions */
angular.module('prototypeApp')
 .controller('PatientConditionCtrl', function ($scope, MedicalRecords) {
        MedicalRecords.success(function(data) {
        $scope.patient = data;
          });
 });

/* FormController */
angular.module('prototypeApp')
 .controller('AddConditionCtrl', function () {
   this.condition = {};

   this.addCondition = function(patient){
     patient.patientCondition.push(this.condition);
   };
 });


/* in my template */

<form name="AddConditionForm" ng-controller="AddConditionCtrl" ng-submit="AddConditionCtrl.addCondition(patient)">
    <input type="text" class="form-control" ng-model="AddConditionCtrl.condition.hcc">
    <input type="text" class="form-control" id="input_description" ng-model="AddConditionCtrl.condition.description">
    <input type="text" class="form-control" ng-model="AddConditionCtrl.condition.last_report">
    <button type="submit" class="btn btn-primary">Add</button>
</form>

<table class="table table-striped">
    <tr>
        <th>Code</th>
        <th>Condition</th>
        <th>Last Reported</th>
        <th>Check</th>
    </tr>
    <tr ng-repeat="condition in patient.patientCondition">
        <td>{{ condition.hcc }} </td>
        <td>{{ condition.description }}</td>
        <td>{{ condition.last_report | date }}</td>
        <td> Check </td>
        </tr>       
</table>
<form action="/#/patient/conditions/ext">
    <button type="submit" class="btn btn-primary pull-right">Next</button>
</form>
/*数据*/
angular.module('prototypeApp')
.factory('MedicalRecords',函数($http){
返回$http.get('scripts/data/patient_record.json');
});
/*为患者状况添加更多信息*/
angular.module('prototypeApp')
.controller('PatientTextConditionCtrl',函数($scope,MedicalRecords){
医疗记录成功(功能(数据){
$scope.patient=数据;
});
});
/*确认病人情况*/
angular.module('prototypeApp')
.controller('PatientConditionCtrl',函数($scope,MedicalRecords){
医疗记录成功(功能(数据){
$scope.patient=数据;
});
});
/*窗体控制器*/
angular.module('prototypeApp')
.controller('AddConditionCtrl',函数(){
this.condition={};
this.addCondition=功能(患者){
病人。病人条件。推(这个条件);
};
});
/*在我的模板中*/
添加
代码
条件
上次报告
检查
{{condition.hcc}}
{{condition.description}}
{{condition.last_report | date}
检查
下一个
你就快到了

您的
addCondition
很好。问题似乎是您混淆了经典的Angular controller语法和较新的“controller as”语法

经典语法

  • 控制器函数获取
    $scope
    作为参数,并向其添加成员
  • 控制器通过
    ng controller=“controllerName”
  • $scope
    的成员可以透明地访问视图(无前缀)

您在前两个控制器中使用经典语法,但随后切换到“控制器为”语法,这是可以的,但您忘记在
ng controller=“AddConditionCtrl”
中指定
作为前缀。不要忘记更新绑定以匹配您选择的前缀。例如,
ng controller=“AddConditionCtrl as addCondition”
意味着您需要
ng model=“addCondition.condition.hcc”

Angular团队建议使用“controller as”语法,因为它使绑定更加明确,因为您可以立即看到哪个字段来自哪个控制器

编辑:我假设您的
表单
元素是具有
ng controller=“PatientExtConditionCtrl”
ng controller=“PatientConditionCtrl”
的元素的后代,因此当
ng submit
执行
addCondition(patient)
时,
patient
变量可用


编辑:有关这两个语法的详细信息可以在中找到。

取决于视图正在执行的操作。请提供视图中的代码(HTML)。@HugoWood我已经更新了模板代码。请在“源代码”框中向下滚动。谢谢你的帮助!谢谢你的详细回答。你完全正确。我不知道控制器呼叫的变化。非常感谢您的回答和帮助。
.controller('controllerName', function ($scope) {
    $scope.thing = 1
})

<div ng-controller="controllerName">{{thing}}</div>
.controller('controllerName', function () {
    this.thing = 1
})

<div ng-controller="controllerName as ctrl">{{ctrl.thing}}</div>