2017-02-17 8 views
1

Ionic Frameworkを使用して基本的なクイズアプリケーションを構築していますが、コントローラ間でパラメータを渡すのに苦労しています。私は$ scopeと$ stateParamsを使ってこれを行う必要があります。どんな助けもありがとう!以下は、私のapp.js、controllers.js、およびQ2.html、私のプロジェクトのHTMLファイルの1つです。コントローラ間でIonic/AngularJSのパラメータを渡します

angular.module('starter', ['ionic', 'starter.controllers', 'starter.services']) 
 

 
.run(function($ionicPlatform) { 
 
    $ionicPlatform.ready(function() { 
 
    // Hide the accessory bar by default (remove this to show the accessory bar above the keyboard 
 
    // for form inputs) 
 
    if (window.cordova && window.cordova.plugins && window.cordova.plugins.Keyboard) { 
 
     cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true); 
 
     cordova.plugins.Keyboard.disableScroll(true); 
 

 
    } 
 
    if (window.StatusBar) { 
 
     // org.apache.cordova.statusbar required 
 
     StatusBar.styleDefault(); 
 
    } 
 
    }); 
 
}) 
 

 
.config(function($stateProvider, $urlRouterProvider) { 
 

 
    $stateProvider 
 

 
    // setup an abstract state for the tabs directive 
 
    .state('tab', { 
 
    url: '/tab', 
 
    abstract: true, 
 
    templateUrl: 'templates/tabs.html' 
 
    }) 
 

 
    // Each tab has its own nav history stack: 
 

 
    .state('tab.dash', { 
 
    url: '/dash', 
 
    views: { 
 
     'tab-dash': { 
 
     params: { 
 
      'Q1Answer': null, 
 
      'Q2Answer': null 
 
     }, 
 
     templateUrl: 'templates/startPage.html', 
 
     controller: 'StartCtrl' 
 
     } 
 
    } 
 
    }) 
 

 
    .state('tab.chats', { 
 
     url: '/chats', 
 
     views: { 
 
     'tab-chats': { 
 
      params: { 
 
      'Q1Answer': null, 
 
      'Q2Answer': null 
 
      }, 
 
      templateUrl: 'templates/Q1.html', 
 
      controller: 'Q1Ctrl' 
 
     } 
 
     } 
 
    }) 
 
    .state('tab.chat-detail', { 
 
     url: '/result', 
 
     views: { 
 
     'tab-chats': { 
 
      params: { 
 
      'Q1Answer': null, 
 
      'Q2Answer': null, 
 
      'total' : null 
 
     }, 
 
      templateUrl: 'templates/result.html', 
 
      controller: 'ResultCtrl' 
 
     } 
 
     } 
 
    }) 
 

 
    .state('tab.account', { 
 
    url: '/account', 
 
    views: { 
 
     'tab-account': { 
 
     params: { 
 
      'Q1Answer': null, 
 
      'Q2Answer': null 
 
     }, 
 
     templateUrl: 'templates/Q2.html', 
 
     controller: 'Q2Ctrl' 
 
     } 
 
    } 
 
    }); 
 

 
    // if none of the above states are matched, use this as the fallback 
 
    $urlRouterProvider.otherwise('/tab/startPage'); 
 

 
});
angular.module('starter.controllers', []) 
 

 
.controller('StartCtrl', function($scope, $state, $stateParams) { 
 

 
    $scope.onStart = function() { 
 
    $state.go('tab.chats', { 
 
     'Q1Answer' : $scope.Q1Answer, 
 
     'Q2Answer' : $scope.Q2Answer 
 
    }) 
 
    }; 
 
}) 
 

 
.controller('Q1Ctrl', function($scope, Chats, $state, $stateParams) { 
 

 
    $scope.Q1Answer = $stateParams.Q1Answer; 
 
    $scope.Q2Answer = $stateParams.Q2Answer; 
 

 
    $scope.Walter = function(answer) { 
 
    $state.go('tab.account', { 
 
     'Q1Answer' : $scope.answer, 
 
     'Q2Answer' : $stateParams.Q2Answer 
 
    }) 
 
    } 
 
}) 
 

 
.controller('ResultCtrl', function($scope, $state, $stateParams, Chats) { 
 

 
    $scope.Q1Answer = $stateParams.Q1Answer; 
 
    $scope.Q2Answer = $stateParams.Q2Answer; 
 

 
    if($scope.Q1Answer == "cat" && $scope.Q2Answer == "1") { 
 
    $scope.total = 2; 
 
    } 
 
    else if ($scope.Q1Answer == "cat" || $scope.Q2Answer == "1") { 
 
    $scope.total = 1; 
 
    } 
 
    else { 
 
    $scope.total = 0; 
 
    } 
 

 
    $scope.done = function() { 
 
    $state.go('tab.dash', { 
 
     'Q1Answer' : "", 
 
     'Q2Answer' : "" 
 
    }) 
 
    } 
 
}) 
 

 
.controller('Q2Ctrl', function($scope, $state, $stateParams) { 
 
    $scope.catCount = function(number) { 
 
    $state.go('tab.chat-detail', { 
 
     'Q1Answer' : $stateParams.Q1Answer, 
 
     'Q2Answer' : $scope.number 
 
    }) 
 

 
    } 
 
});
<ion-view view-title="Text Question"> 
 
    <ion-content> 
 
    <div> 
 
     <h2>How many cats were in the last photo?</h2> 
 
    </div> 
 
    {{Q1Answer}} 
 
    {{Q2Answer}} 
 
    <div class="bottom"> 
 
     <div style="float: left; width: 50%;"> 
 
     <label class="item item-input"> 
 
      <input type="text" name="number" id="number-textarea" 
 
       ng-model="$parent.number" placeholder="Enter Your Answer"> 
 
     </label> 
 
     </div> 
 
    </div> 
 
    <button class = "button button-full" ng-click = "catCount(number)">Submit</button> 
 
    </ion-content> 
 
</ion-view>

答えて

0

コントローラ

.state('tab.dash', { 
    url: '/dash', 
    views: { 
     'tab-dash': { 
     params: { 
      'AnswerData': null 
     }, 
     templateUrl: 'templates/startPage.html', 
     controller: 'StartCtrl' 
     } 
    } 
    }) 
経路においてこの

を試してください。他の制御において

$scope.Walter = function(answer) { 
    var AnswerData = { 
     'Q1Answer' : 'data1', 
     'Q2Answer' : 'data2'   
    } 
    $state.go('tab.account',AnswerData); 
    } 

経路における充填剤

$scope.AnswerData = $stateParams.AnswerData; 
//i.e. $stateParams.AnswerData = { 
    // 'Q1Answer' : 'data1', 
    // 'Q2Answer' : 'data2'   
    // } 
0

chnage:

.state('tab.chats', { 
       url: '/chats', 
       views: { 
        'tab-chats': { 
         templateUrl: 'templates/Q1.html', 
         controller: 'Q1Ctrl' 
        } 
       }, 
       params: { 
        'Q1Answer': null, 
      'Q2Answer': null 
       } 
      }) 

パスパラメータで:コントローラ内

$scope.Q1Answer = {} // your object 
$scope.Q2Answer = {} // your object 

$state.go('tab.chats', { 
    'Q1Answer' : $scope.Q1Answer, 
    'Q2Answer' : $scope.Q2Answer 
}) 

.controller('Q1Ctrl', function($scope, Chats, $state, $stateParams) { 

    $scope.Q1Answer = $stateParams.Q1Answer; 
    $scope.Q2Answer = $stateParams.Q2Answer; 
    console.log($scope.Q1Answer) 
console.log($scope.Q2Answer) 
}) 
関連する問題