Attach Custom Data to State Objects -Ionic

Hallo Guys,

Can someone please show me how to attach custom data on State Objects and retrieve the data in Ionic template/view?

Here’s the documentation:

ngRouter Custom data link

I tried the way of attaching custom data to state on ngRouter as mentioned in the Docs but on retrieving the data ,it throws an error and the whole screen goes white.

This line to log data is probably the cause of error:

console.log($state.current.data.customData1)

Thanks in advance.

Naveen

what have you tried? Can you put some code in CodePen? Or post something to start with here in your question?

How it works is described in the ui-router doc.
(And this works very well in my apps).

We need your code to say what is possibly wrong.

HI, did you mean bind an object to the state so that you can retrieve this object in your controller?

If so, you may looking for the resolve, one of the configure property in $stateProvider.state() method, try

$stateProvider.state('some-route', {
  // ...
  resolve: {
    customData: function () {
        // return your custom object here
    }
  }
});

angular.controller('SomeController', ['$scope', 'customData', function ($scope, customData) {
  // now you can retrieve your customData and then use it to do something stuff
}]);

Good day

yeah but you can simply add

$stateProvider.state('some-route', {
// ...
     customData: 'hello' //what ever you want
  }
});

And on the state-objects you can access “customData”.

resolve is nice for async stuff like requests.

1 Like

:smile: Thank you to figure out

Thanks for the reply guys. I solved the issue,it was just wrong coding with ‘view’ and ‘data’ property.