Daniel
Daniel

Reputation: 269

Simplest way to pass value from url to the content of page in ionic framework using angularjs

I am using ionic framework for developing my hybrid mobile application and I have to access a content editing page where I need to pass in some values. For this, I have added the parameters in the url in routes.js routing file

.state('editText', {
    url: '/editText/:id/:text/:size',
    templateUrl: 'editText.php',
    controller: 'editTextCtrl'
  })

Now I want to access :id , :text and :size in the content of the page. Can I simply do it by using {id} , {text}, {size} in the content of page or do I need to do something else to make it happen. Please note that at this point I am looking for the fastest and simplest solution to achieve this without caring for security.

Upvotes: 1

Views: 2932

Answers (2)

Jur Clerkx
Jur Clerkx

Reputation: 698

You can request the URL state parameters using $stateParams. Make sure you inject this into your controller.

Let's say you state url is /editText/:id/:text, then you can request the values from the url in your controller (editTextCtrl) using $stateParams.id and $stateParams.text.

Upvotes: 1

Venu prasad H S
Venu prasad H S

Reputation: 241

change your state as follows,

 .state('editText', {
        url: '/editText',
        templateUrl: 'editText.php',
        controller: 'editTextCtrl',
        params: {
            id:'',
            text:'',
            size:''
        }
      })

You can call the state from controller as follows to pass the parameters,

$state.go('editText',{id:45,text:'some text',size:45});

Then you can get these parameters in the controller as follows,

.controller('editTextCtrl',['$stateParams',function($stateParams){
    console.log($stateParmas.id);
    console.log($stateParmas.text);
    console.log($stateParmas.size);
}]);

Upvotes: 0

Related Questions