1

My goal to achieve is:

first to insert new database record with http post, resolve with stateProvider and grab the new id and change view and stateParams.

i have this code for my http post service

myApp.service('postService', ['$http', function($http) {
this.insertNew = function() {
     $http.post('create_new.php')
    .success(function(data) {
        return data;
    });
};

create_new.php returns the ID like this (it works, proved with console)

return json_encode($data);

and the stateProvider looks like this (section)

 $stateProvider
 .state('newRecord', {
    resolve: {
        newArtID: ['postService',
            function(postService) {
                 return postService.insertNew();
        }]
    },
    params: {
        artID: <-- new ID from DB
    },

i did tests with stateParams in serval variations (in resolve and by params). how can i bring the new ID to stateParams, so i can access from the views?

Thanks for any help!

2
  • Firstly, this seems like a bad idea. If you want to do this, you can create an abstract state above newRecord, do the resolve in there, then $state.go to the child state that has the parameter Commented Jul 17, 2015 at 14:11
  • I`m new to angular & use ui-router the first time. I will try your suggestion. Thanks Commented Jul 17, 2015 at 14:35

1 Answer 1

0

I'm not so sure your oder of operations is correct. params is for when you already have that data. You should return the data from your resolve, then you can access it in your scope, for ex:

Service:

.service('postService', function ($http) {
    this.insertNew = function () {
        return $http.post('create_new.php').then(function (data) {
            return data;
        });
    }
})

Route:

    $stateProvider
        .state('newRecord', {
            views: {
                "main": {
                    controller: 'SomeCtrl',
                    templateUrl: '...'
                }
            },
            resolvedId: {
                newArtID: function (postService) {
                    return postService.insertNew().then(function (response) {
                        return response;
                    });
                }
            }
        })

Controller:

    .controller('SomeCtrl', function (resolvedId) {
        var newID = resolvedId.id;  //depending on what is returned
    });
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your help. Meanwhile i did a workaround where works. But maybe i can use this in a other project.

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.