javascript - Manipulate saved object after ngResource $save -
so i'm trying $save object , empty it's values after.
i've got:
$scope.newobject = new objectservice() $scope.saveobject = function(objecttosave)
so ng-click="saveobject(newobject)
calls function passes object , i'm trying empty it's values after $save done. way want initialise objecttosave
new objectservice()
again (as before $save()). problem doesn't work (i know has javascript fundamentals, don't know is).
please check code snippet bellow. code comments explain issue better.
please note ! : i'm trying handle response of $save in .error() function because example.
angular.module('app', ['ngresource']) .controller('ctrl', ['$scope', 'objectservice', function($scope, objectservice) { $scope.newobject = new objectservice(); $scope.newobject.foo = 'something'; $scope.saveobject = function(object) { object.$save(function() { // won't success in example.. }, function() { //object.foo = '...'; // works fine. object = new objectservice(); // $scope.newobject shouldn't new objectservice now? $scope.newobject.foo should empty string }); }; } ]) .factory('objectservice', ['$resource', function($resource) { return $resource('http://somewhere/something', { foo: '@foo' }); } ]);
<html ng-app="app"> <body ng-controller="ctrl"> <input type="text" ng-model="newobject.foo"> <button type="button" ng-click="saveobject(newobject)">save</button> <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> <script src="https://code.angularjs.org/1.3.15/angular-resource.min.js"></script> </body> </html>
object = new objectservice();
won't work because way arguments passed functions in javascript. see https://developer.mozilla.org/en-us/docs/web/javascript/reference/functions
in case, can assign $scope.newobject new empty object.
$scope.newobject = new objectservice();
you don't need argument because can use variable $scope.newobject call resource methods.
Comments
Post a Comment