0

I have a problem with my CRUD operations in the service. When I click on Create Btn it is creating an object but it doesn't push the object in the table list.

Ctrl (where's the table list):

$scope.nameslist = CrudService.getAll();

Ctrl (for Modal dialog):

$scope.createItem = function (newObj) {
   CrudService.create(newObj);
   $scope.newObj = null;
   $scope.ok();
}

CRUD Service (it's a .factory):

...
return {
    getAll: function () {
       return resService.names.query();
    },

    create: function (newObj) {
       resService.names.save(newObj);
       //this.getAll.push(newObj); //this doesn't work
    }
...

Request Service (also a .factory):

...
return {
  names: $resource(baseUrl + '/api/names/:Id', {
      Id: '@Id'
  }, {
     'update': {
          method: 'PUT'
     }
  })
...

Can anyone help me? How can I push the new object in the table list?

1
  • Either you push your new item manually or call the getAll function again to get the updated item from server. Commented Jun 17, 2015 at 15:57

1 Answer 1

2

After you've created the object you can push the object to the list or call getAll

$scope.createItem = function (newObj) {
   CrudService.create(newObj);
   $scope.newObj = null;
   $scope.ok();
   \\either
   $scope.nameslist = CrudService.getAll();
   \\or
   $scope.nameslist.push(newObj); // this assumes this is an array
}

UPDATE/// $broadcast sends messages down to child controllers whereas $emit sends them up. using $rootscope.$emit first inject it into the controller

.controller('myCtrl' ['$scope', '$rootscope', function($scope, $rootscope ...

then you can use $rootscope.$emit('added-Name') or you can even add an argument so $rootscope.$emit('added-Name', {newObj: newobj})

then in the catching controller

$rootscope.$on('added-Name'), function(event, args) {
    $scope.namelist.push(args.newObj);
    //or if you're not passing the argument
    $scope.nameslist = CrudService.getAll();
});

using a Shared Service:

angular.module('myApp').service('sharedService', function(){
    var nameList = [];
    return{
        get: function(){
            return nameList;
        }
        set: function(val){
            nameList = val;
        }
        add: function(name){
            nameList.push(name);
        }
    }
})

inject the sharedservice into the controller `.controller('ctrl', ['$scope', 'sharedService', function($scope, sharedService ....

fill the nameList in the service with sharedService.set(CrudService.getAll()); and in $scope.createItem you could have sharedService.add(newObj);

then you can have a watch on the sharedService.get()

$scope.$watch(function() {
            return sharedService.get();
        }, function(newValue, OldValue) {

            if (newValue !== OldValue) {
                $scope.namesList = sharedService.get();
            }
        });
Sign up to request clarification or add additional context in comments.

10 Comments

Is this good practice? I thought this would be defined in the service?!
the get? then use your getAll(). That is the best way as you're keeping in sync with the server. I was just demonstrating that you could add the object directly into your list.
no I mean $scope.nameslist.push(newObj) Is there an opportunity to define this in the service?
The $scope.nameslist is in an other Ctrl as well than the $scope.createItem
ah i see. looks like you'll require some way of telling the table list Ctrl to get the list again. You could you $rootscope.$broadcast or $rootscope.$emit to send a signal throughout the app or you could have a service to hold the nameslist and $watch for changes.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.