Gustavo Martins
Gustavo Martins

Reputation: 11

Params with $state.go and $scope

I tried many things to pass data to my view, but i don't know what is wrong. I tried with $scope and pass data with $state.go, but nothing happens.

  .state('app.ler',
                            {
                                views: {
                                    'menuContent': {
                                        templateUrl: 'mensagens/mensagemRead.html',
                                        controller: 'MensagensCtrl',
                                        params: {'assunto': null}
                                    }
                               
                                },
                                url: '/ler'
                            })
  
     $scope.read = function (id) {
            MensagensService.ler(id).then(function (response) {
//                $scope.mensagem_ler = response.data[0];
//                alert(response.data[0].assunto);
                $state.go('app.ler', {assunto:response.data[0].assunto});
                console.log($stateParams);
            });
        };

<ion-view title="Mensagens2">    
    <ion-content class="has-header">
        <div class="list">
            <a class="item item-avatar" href="#">
                <img src="venkman.jpg">
                <h2>Gustavoaaa</h2>
                <!--<p>{{mensagem_ler.assunto}}</p>-->
<p>{{assunto}}</p>
            </a>
        </div>
    </ion-content>
</ion-view>

Upvotes: 1

Views: 944

Answers (3)

Pankaj Parkar
Pankaj Parkar

Reputation: 136144

params option of state is available in root level of state definition, you could not have them in leaf level of namedView.

.state('app.ler', {
     views: {
         'menuContent': {
             templateUrl: 'mensagens/mensagemRead.html',
             controller: 'MensagensCtrl',
         }
     },
     url: '/ler/{assunto}', //here is parameter mentioned in URL
     params: {
         'assunto': null //parameter default value
     }
 })

You can get this values in $stateParams object inside your controller.

Upvotes: 1

Max Hershey
Max Hershey

Reputation: 21

You can add the state parameter using a : in the url of your state.

.state('app.ler',
    {
         views: {
             'menuContent': {
                  templateUrl: 'mensagens/mensagemRead.html',
                  controller: 'MensagensCtrl'
               }

         },
         url: '/ler/:assunto'
});

This will make the property assunto available in $stateParams

Upvotes: 1

Vandervidi
Vandervidi

Reputation: 692

Try this in your state configurations

url: '/ler?assunto'

If you want to pass more parameters then just add them like this

 url: '/ler?assunto&param2&param3....'

To use the state param in your view you can save it in a $scope variable in your controller and then use it.

For example: $scope.assunto = $stateParams.assunto;

<ion-view title="Mensagens2">    
    <ion-content class="has-header">
        <div class="list">
            <a class="item item-avatar" href="#">
                <img src="venkman.jpg">
                <h2>Gustavoaaa</h2>
                <!--<p>{{mensagem_ler.assunto}}</p>-->
<p>{{assunto}}</p>
            </a>
        </div>
    </ion-content>
</ion-view>

Upvotes: 0

Related Questions