2016-09-16 47 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