1

Now I need switch view by using $state.go, but I need to pass some data in it. (I perfer to pass a object) I searched on line for help, but didn't see a good answer which fit my question. Here is my code:

state('editreport', {
        url: '/editreport/:reports',
        templateUrl: 'submitter/editreport/editexpensesreport.html'
    })

in my Ctr1:

$scope.goeditpage = function(report){

                            $state.go('editreport',{'reports':report});
                        }

when user fire goeditpage . it will take them to 'editreport' page.and in editreport page, I have another controller called 'ctr2', I try to access data like this:

controller('Ctr2',
        [ '$scope','$stateParams', function($scope,$stateParams) {
            $scope.lin = $stateParams.reports;
            console.log($scope.lin);
        } ])

but didn't work out, any idea? and another question is how to make data optional? because user may enter data or not, but it will always take them to this page.

linyuanxie
  • 777
  • 4
  • 13
  • 31
  • 1
    What is the error you get? – Sam Mar 31 '15 at 22:39
  • I *think* the issue may be due to the `url` property in your state definition. Could the answer provided here: http://stackoverflow.com/q/19516771/355627 provide any help? – Smokin Joe Mar 31 '15 at 22:42
  • did you provide a controller for the `editreport` state? I don't see one – Ronnie Mar 31 '15 at 22:51
  • @Ronnie Ctr2 - its mentioned. – Sam Mar 31 '15 at 22:52
  • @Sam I see Ctr2 but how is that state going to know what controller to load without the controller property? Am I missing something? The state should be http://pastie.org/10065807 – Ronnie Mar 31 '15 at 22:55
  • You don't necessarily need it to be explicitly mentioned in the states. It must be referenced in his html file. Anyways, good point to check on. – Sam Mar 31 '15 at 22:59
  • _"Parameter names may contain only word characters (latin letters, digits, and underscore) and must be unique within the pattern (across both path and search parameters)."_ [basic parameters](https://github.com/angular-ui/ui-router/wiki/URL-Routing#basic-parameters) Instead you should use [query parameters](https://github.com/angular-ui/ui-router/wiki/URL-Routing#query-parameters) – Chris Traveis Mar 31 '15 at 23:04
  • The things is in Ctr2, when I console out, it shows[object object],no error shown, this is confused part I got. Actually Do I have to pass it through URL? because the data could be large, the url will look terrible. – linyuanxie Mar 31 '15 at 23:05
  • @linyuanxie The question is do you want it to be a part of url or are you just using this as a means of data passing ? – Sam Mar 31 '15 at 23:08
  • I don't want it to be part of URL. – linyuanxie Mar 31 '15 at 23:10
  • Then better option would be to use a service and inject in both your controllers. You can update a variable in that service from Ctr1 and then access it from Ctr2. – Sam Mar 31 '15 at 23:13
  • If you want just share data across controllers look here: http://stackoverflow.com/questions/29099969/angularjs-pass-value-from-one-page-to-another/29100211#29100211 – Zeeshan Hassan Memon Mar 31 '15 at 23:42
  • Thanks Zeeshan, using services is real good option, but I am still curious, if there is any way I can pass through $state.go? I am just curious. – linyuanxie Apr 01 '15 at 14:37

1 Answers1

0

A way to do this is pass that data through the query string, which is what $state.go('state-name', {name:'fizz', tag: 'stackoverflow'})

on your state it needs to be like this:

$stateProvider.state('editreport', { url: '/editreport/:reports?name=&tag', templateUrl: 'submitter/editreport/editexpensesreport.html' })

on your controller you will need to retrieve that information, you can use $location for that, like this:

var myParams = $location.search(); //$location.search() returns an object that will look like this myParams = {name: 'fizz', tag: 'stackoverflow'} After copying the parameters you can delete them from the url with: $location.search({});