0

I have 2 pages and I want to pass a variable from page 1 to page 2.

page1.html

<ion-view ion-title="Home">
<ion-content>
    <label class="item item-input">
    <input type="text" placeholder="Enter a value" ng-model="value">
    </label>

    <button class="button button-block button-positive" ng-click="me(value)" >
    Go to List
    </button>

</ion-content>
</ion-view>

this is the controller , I use $state to go to page 2 and I use $rootscope to be able to use the value in the page2Controller

angular.module('starter.controllers', [])
.controller('page1Ctrl', function($rootScope ,$state ) {

    $rootScope.me = function(value)
    {
        $rootScope.value= value;
        $state.go('page2');
    };

});

the problem is , when I click the button in page 1 it doesn't take me to page2 where is the mistake

this is states code

.config(function($stateProvider, $urlRouterProvider){
$stateProvider  
.state('page1', {
    url: '/page1',
    templateUrl: 'templates/page1.html',    controller: 'page1Ctrl'})   

.state('page2', {
    url: '/page2',  
    templateUrl: 'templates/page2.html',    controller: 'page2Ctrl'})

$urlRouterProvider.otherwise('/page1');})
Aravind
  • 40,391
  • 16
  • 91
  • 110
ca2264
  • 1
  • 2

1 Answers1

3

You did not add dependency to

angular.module('starter.controllers', ['ui.router'])
.controller('page1Ctrl', function($rootScope ,$state ) {

    $rootScope.me = function(value)
    {
        $rootScope.value= value;
        $state.go('page2');
    };
});

and reference the js file in your html file.

or

In case of ionic framework I would suggest you to go with ion-nav-view in this link which will reduce your efforts to a greater extent, where your module is dependent on ionic as below

angular.module('starter', ['ionic'])
Aravind
  • 40,391
  • 16
  • 91
  • 110
  • It's possible OP has a separate main module (guessing it's called "starter") and it does include `ui.router`. This is still good advice though as module's should be stand-alone and include all required dependencies – Phil Nov 02 '16 at 00:24
  • Yes but no where in the code, there is a dependency to **ionic**. Thank you Phil. Correct me if I am wrong – Aravind Nov 02 '16 at 00:25
  • Depends, OP hasn't shown the module declaration for that `config` function. You may very well be correct but it's impossible to tell until OP comes back and adds more information – Phil Nov 02 '16 at 00:27
  • Yes. Exactly Correct. – Aravind Nov 02 '16 at 00:28