AngularJS 对象差异
AngularJS object differences
我正在努力思考 JavaScript 个对象,并努力了解两个对象有何不同。
我有一个用于将记录添加到数据库的输入表单和一个使用 $resource $save() 进行添加的控制器。
在我显示输入表单之前,如果我像这样定义表单对象 $scope.formUser = new Users();
$save 工作。
如果我像这样定义表单对象 $scope.formUser = {};
$save 会给我这个错误:$scope.formUser.$save is not a function
如果我这样做 console.log(JSON.stringify($scope.formUser));
两个对象对我来说看起来完全一样:
$scope.formUser = new Users();
显示 {"firstname":"aaa","lastname":"bbb"}
$scope.formUser = {};
显示 {"firstname":"aaa","lastname":"bbb"}
为什么一个对象保存而另一个不保存?
是否可以创建一个不使用 $scope.formUser = new Users();
也能正常工作的对象?
这是我的代码...
angular.module('starter.services', [])
.factory('Users', function ($resource) {
return $resource('https://example.com/:id', { id: '@id' }, {
update: { method: 'PUT' }
});
})
.controller('myController', function ($scope, $ionicPopup, Users) {
// Triggered by clicking the 'add user' button in header.html
$scope.addUser = function () {
//$scope.formUser = {}; // this doesn't work
$scope.formUser = new Users(); // this works
// Show popup form
$ionicPopup.show({
templateUrl: 'templates/userform.html',
title: 'Add new user',
subTitle: 'Firstname is mandatory',
scope: $scope,
buttons: [
{ text: 'Cancel' },
{
text: '<b>Save</b>',
type: 'button-positive',
onTap: function (e) {
if (!$scope.formUser.firstname) {
e.preventDefault(); // Prevent save if first name is empty
} else {
$scope.formUser.id = 0; // If id is left undefined, $save sends a GET method
$scope.formUser.$save()
}
}
}
]
});
};
})
当您 return 资源时,它 return 是一个资源对象,它具有 $save
作为 属性。做 $scope.formUser = {}
是在创建一个空对象。
我正在努力思考 JavaScript 个对象,并努力了解两个对象有何不同。
我有一个用于将记录添加到数据库的输入表单和一个使用 $resource $save() 进行添加的控制器。
在我显示输入表单之前,如果我像这样定义表单对象 $scope.formUser = new Users();
$save 工作。
如果我像这样定义表单对象 $scope.formUser = {};
$save 会给我这个错误:$scope.formUser.$save is not a function
如果我这样做 console.log(JSON.stringify($scope.formUser));
两个对象对我来说看起来完全一样:
$scope.formUser = new Users();
显示 {"firstname":"aaa","lastname":"bbb"}
$scope.formUser = {};
显示 {"firstname":"aaa","lastname":"bbb"}
为什么一个对象保存而另一个不保存?
是否可以创建一个不使用 $scope.formUser = new Users();
也能正常工作的对象?
这是我的代码...
angular.module('starter.services', [])
.factory('Users', function ($resource) {
return $resource('https://example.com/:id', { id: '@id' }, {
update: { method: 'PUT' }
});
})
.controller('myController', function ($scope, $ionicPopup, Users) {
// Triggered by clicking the 'add user' button in header.html
$scope.addUser = function () {
//$scope.formUser = {}; // this doesn't work
$scope.formUser = new Users(); // this works
// Show popup form
$ionicPopup.show({
templateUrl: 'templates/userform.html',
title: 'Add new user',
subTitle: 'Firstname is mandatory',
scope: $scope,
buttons: [
{ text: 'Cancel' },
{
text: '<b>Save</b>',
type: 'button-positive',
onTap: function (e) {
if (!$scope.formUser.firstname) {
e.preventDefault(); // Prevent save if first name is empty
} else {
$scope.formUser.id = 0; // If id is left undefined, $save sends a GET method
$scope.formUser.$save()
}
}
}
]
});
};
})
当您 return 资源时,它 return 是一个资源对象,它具有 $save
作为 属性。做 $scope.formUser = {}
是在创建一个空对象。