【发布时间】:2019-07-29 21:04:19
【问题描述】:
抱歉,如果这很简单,我对环回/后端比较陌生,
我正在尝试使用以下代码更新现有数据库记录 ID 和名称。 HTML 文件
<div>
<h1>COMPANY DETAILS</h1>
</div>
<div>
<div>
<table>
<thead>
<tr>
<th>Company Name</th>
<th>Company Code</th>
<th>Remove</th>
<th>Edit</th>
</tr>
</thead>
<tbody>
<tr>
<td><input class="form-control" ng-model="newCompany.name"></td>
<td><input class="form-control" ng-model="newCompany.id"></td>
<td><button class="btn btn-primary" ng-click="add()">Add</button></td>
<td><button class="btn btn-info" ng-click="update()">Update</button></td>
</tr>
<tr ng-repeat="company in companies | orderBy:'+name'">
<td>{{company.name}}</td>
<td>{{company.id}}</td>
<td><button class="btn btn-danger" ng-click="remove(company.id)">Remove</button> </td>
<td><button class="btn btn-warning" ng-click="edit(company)">Edit</button> </td>
</tr>
</tbody>
</table>
</div>
</div>// This is just a sample script. Paste your real code (javascript or HTML) here.
if ('this_is'==/an_example/){of_beautifier();}else{var a=b?(c%d):e[f];}
JS 文件
'use strict';
/**
* @ngdoc function
* @name abcApp.controller:CompanyCtrl
* @description
* # CompanyCtrl
* Controller of the abcApp
*/
angular.module('abcApp')
.controller('CompanyCtrl', ['$scope', 'Company', function ($scope, Company) {
$scope.newCompany = { 'id': '', 'name': '' };
$scope.companies = [];
Company
.find()
.$promise
.then(function (results) {
$scope.companies = results;
});
$scope.update = function (company) {
company.findById({ id: company })
.$promise
.then(function () {
$scope.company.id = 13.40;
console.log(company.id);
$scope.company.$save();
});
};
$scope.edit = function (company) {
$scope.newCompany = { id: company.id, name: company.name };
}
$scope.add = function () {
Company.create($scope.newCompany, function (company) {
$scope.newCompany = { 'id': '', 'name': '' };
Company
.find()
.$promise
.then(function (results) {
$scope.companies = results;
});
}, function (errorResponse) {
console.log(errorResponse);
});
};
$scope.remove = function (cid) {
Company.deleteById({ id: cid })
.$promise
.then(function () {
console.log('deleted');
Company
.find()
.$promise
.then(function (results) {
$scope.companies = results;
});
});
}
}]);
$scope.edit 函数将公司 ID 和名称放入两个文本框中,$scope.update 函数用于更新数据库记录,编辑函数工作正常但是我的 $scope.update 出现问题,当我单击更新按钮,我在浏览器控制台中收到以下错误。
无法设置未定义或空引用的属性“名称”
很抱歉,很长的帖子,任何帮助将不胜感激
【问题讨论】:
标签: javascript angularjs mean-stack loopbackjs