我建立一个客户端收集以下信息:更新数组内的对象AngularJS对象内
"name" : "Test client",
"email" : "[email protected]",
"position" : "Project Manger",
"contacts" : [
{
"name" : "asdf",
"email" : "[email protected]",
"tel" : "7877877878",
"title" : "asdf"
},
{
"name" : "fdas",
"email" : "[email protected]",
"tel" : "7877877878",
"title" : "fdsa"
}
],
我希望能够编辑/更新客户端的接触,但我我不知道如何做到这一点,因为我有一个ng-repeat内部的表单,重复客户端的联系。
<div ng-repeat="contact in contacts track by $index">
<label>Name</label>
<input type="tel" ng-model="contact.name">
<label>Telephone</label>
<input type="tel" ng-model="contact.tel">
<label>Email</label>
<input type="email" ng-model="contact.email">
<label>Title</label>
<input type="text" ng-model="contact.title">
<md-button ng-click="save(contact)">Save</md-button>
</div>
和我的控制器:
'use strict'
angular.module('myApp')
.controller('ContactsCtrl', function($scope, $mdDialog, $mdMedia, client) {
$scope.client = client;
$scope.contacts = client.contacts;
$scope.save = (contact) => {
Clients.update({_id: client._id},{
$set: {
contacts : contact
}
},function(err, data){
if(err) return console.log(err);
console.log(data + " ");
$mdDialog.hide(data);
});
}
$scope.cancel = function() {
$mdDialog.cancel();
};
$scope.hide = function() {
$mdDialog.hide();
};
});
但是,当我按下save
它取代单个对象的数组。
问题
我如何更新阵列是一个文档内有一个形式,是内部的NG-重复现有的对象?
嗨,你的代码是有道理的,但我不断收到语法错误at'contacts [index]:contact' –