angularjs - Example of using a parameter passed through $state.go -
can explain how use parameters sent $state.go? in createpollcontroller create poll send state add_data_poll
(adddatapollcontroller
), don't know how access parameter display in view or use in controller (i tried see response console.log($scope.response)
, doesn't work), can explain me?
angular.module('estudios') .controller('createpollcontroller', ['$scope', 'restangular', '$state', function($scope, restangular, $state) { $scope.addpoll = function() { if ($scope.allow_anonymous_answer == null) $scope.allow_anonymous_answer = false var poll = {title: $scope.title, description: $scope.description, allow_anonymous_answer: $scope.allow_anonymous_answer, initial_message: $scope.initial_message, final_message: $scope.final_message}; restangular.all('polls').post(poll).then(function(response) { $state.go('add_data_poll', response); }); }; }]) .controller('adddatapollcontroller', ['$scope', 'restangular', function($scope, restangular) { }]);
and these corresponding states.
.state('create_poll', { url: '/create_poll', parent: 'home', templateurl: 'poll/create_poll.html', controller: 'createpollcontroller' }) .state('add_data_poll', { url: '/create_poll/add_data_poll', parent: 'home', templateurl: 'poll/add_data_poll.html', controller: 'adddatapollcontroller'
you need have either params
defined in state or query params defined in state url.
example state params
:
.state('add_data_poll', { url: '/create_poll/add_data_poll', params: { // define object parameters want pass // example: id: 1 // 1 default parameter if no id passed } parent: 'home', templateurl: 'poll/add_data_poll.html', controller: 'adddatapollcontroller'
this way can send parameters won't available in query string , upon refresh lost.
example defining query parameters:
.state('add_data_poll', { url: '/create_poll/add_data_poll?someparameter&anotherone', parent: 'home', templateurl: 'poll/add_data_poll.html', controller: 'adddatapollcontroller'
someparameter
, anotherone
available if pass them incoming state.
when passing parameters should define parameters passing.
$state.go('some.route', {id: 2, someparam: 'coolparam');
and can access them $stateparams
in controller. first need inject it.
myapp.controller('myctrl', function($stateparams) { console.log($stateparams); });
it not idea pass whole response api. better if choose stuff need response , build state around them.
read more ui-router
state
params
here
Comments
Post a Comment