2016-09-16 126 views
0

我已经定义了这种状态:如何访问状态视图的自定义属性?

.state('app.feedback', { 
     url: '/feedback', 
     views: { 
      'appContent': { 
       templateUrl: 'templates/feedback.html', 
       controller: 'feedbackCtrl', 
       customParameter: 'This is my Name' 
      } 
     } 
    }) 

我要表明customParameterappContent

这是我的控制器:

.controller('feedbackCtrl', 
    ['$scope', '$log', '$state', 
function($scope, $log, $state) { 

    $scope.customParameter = $state.current.customParameter; 

}]); 

,这是我的看法:

<ion-view view-title="{{customParameter}}"> 
    <ion-content> 
     <h1>{{customParameter}}</h1> 
    </ion-content> 
</ion-view> 

没有大干快上的观点表明,当我对国家的声明中使用自定义的参数,而视图对象它工作,但我需要在这个项目中有几个意见。我如何访问该自定义参数?

PS:我与离子的工作1.3.1

回答

0

我用来试图读取参数的rute是错误的。我需要在$state对象中挖掘更多信息才能找到正确的方法。

所有参数都内$state.current

甲一个示例(基于所述离子起动器侧菜单上)制成的plunker

状态:

.state('app.playlists', { 
     url: "/playlists", 
     views: { 
     'menuContent' :{ 
      templateUrl: "playlists.html", 
      controller: 'PlaylistsCtrl', 
      menuContentParam:'menuContentParam' 
     }, 
     viewsParam:'viewsParam' 
     }, 
     stateParam:'stateParam' 
    }); 

的控制器

.controller('PlaylistsCtrl', function($scope, $state) { 
$scope.stateParam = $state.current.stateParam; 
    $scope.viewsParam = $state.current.views.viewsParam; 
    $scope.menuContentParam = $state.current.views.menuContent.menuContentParam; 
}) 

并查看:

<ion-view title="Playlists"> 
    <ion-nav-buttons side="left"> 
    <button menu-toggle="left" class="button button-icon icon ion-navicon"></button> 
    </ion-nav-buttons> 
    <ion-content class="has-header"> 
    <ion-list> 
     <ion-item>{{stateParam}}</ion-item> 
     <ion-item>{{viewsParam}}</ion-item> 
     <ion-item>{{menuContentParam}}</ion-item> 
    </ion-list> 
    </ion-content> 
</ion-view> 
2

使用状态PARAMS到自定义数据传递给状态

.state('app.feedback', { 
    url: '/feedback', 
    views: { 
     'appContent': { 
      templateUrl: 'templates/feedback.html', 
      controller: 'feedbackCtrl' 
     } 
    }, 
    params:{ 
    customParameter: 'This is my Name' 
    } 
}) 

在控制器使用$ stateParams得到customParameter

.controller('feedbackCtrl', 
    ['$scope', '$log', '$state', $stateParams 
function($scope, $log, $state, $stateParams) { 

    $scope.customParameter = $stateParams.customParameter; 

}]); 

而视图是

<ion-view view-title="{{customParameter}}"> 
    <ion-content> 
     <h1>{{customParameter}}</h1> 
    </ion-content> 
</ion-view> 
+0

实际上,当我看到$ stateParams对象是空的,我认为它只适用于URL制作参数。 – distante