在ionic1的状态下,如何将$ stateParams传递给视图?

时间:2018-04-24 08:45:28

标签: javascript ionic-framework

例如,有一个状态,它控制2个视图:

WWW / index.html中

<html>
  .
  .
  .
  <body ng-app='starter' ng-controller="MainCtrl"><p>
    <ui-view name="redView" style="color:red;"></ui-view>
    <ui-view name="greenView" style="color:green;"></ui-view>
  </body>
</html>

WWW / JS / app.js

angular.module('starter',['ionic'])
.config(function($stateProvider, $urlRouterProvider) {
  $stateProvider
    .state('contacts',{
      'views':{
        'redView':{
          template:'<p>Red View</p>',
          controller:function($scope,$stateParams){
            console.log($stateParams.redString);
          }
        },
        'greenView':{
          template:'<p>Green View</p>',
          controller:function($scope,$stateParams){
            console.log($stateParams.greenString);
          }
        }
      }
    });
})
.controller('MainCtrl', function($scope,$state) {
  $state.go('contacts',{redString:'abc',greenString:'def'});
});

我传递参数&#34; redString&#34;和&#34; greenString&#34;到州&#39;联系人&#39;,但

console.log($stateParams.redString);

console.log($stateParams.greenString);

打印undefined。我究竟做错了什么?怎么让

console.log($stateParams.redString);

打印&#39; abc&#39;成功?

1 个答案:

答案 0 :(得分:0)

您必须为状态对象

中的参数声明默认值
.state('contacts',{
  'views':{
    'redView':{
      template:'<p>Red View</p>',
      controller:function($scope,$stateParams){
      console.log($stateParams.redString);
     }
   },
  'greenView':{
     template:'<p>Green View</p>',
     controller:function($scope,$stateParams){
     console.log($stateParams.greenString);
     }
   },
   params: {
     //here
     redString: null,
     greenString: null
   }
 })



.controller('MainCtrl', function($scope,$state) {
  $state.go('contacts',{redString:'abc',greenString:'def'});
});