2016-01-20 29 views
1

我遇到了一個問題,試圖將參數對象傳遞給使用stage.go()的狀態。angularjs狀態參數不起作用

這裏是我的狀態定義:

.state('drillhole.ddhinttype', { 
     url: '/ddhinttype', 
     templateUrl: VIRTUAL_DIR_PATH + '/App/Views/drillholemanager/drillhole/tabddhinttype.html?v=' + fileVer, 
     controller: 'DrillHoleDdhIntTypeController', 
     params: { name: null, description: null } 
    }) 

這裏是我的控制器:

try { 
    angular.module('centric.drillhole.manager'); 
} catch (e) { 
    angular.module('centric.drillhole.manager', ['app.config', 'ui.router', 'kendo.directives', 'ui.bootstrap', 'ngCookies', 'centric.common', 'centric.notification', 'pascalprecht.translate', 'centric.security', 'centric.app.settings']); 
} 

angular.module('centric.drillhole.manager').controller('DrillHoleDdhIntTypeController', ['$scope', 'CentricUIHelper', 'NumberHelper', 'DrillHoleManagerService', 'app.config', '$stateParams', 
function ($scope, uihelper, numberHelper, service, appconfig, $stateParams) { 

    $scope.loading = false; 

    $scope.isbusy = function() { 
     return $scope.loading || $scope.$parent.loading; 
    } 

    var load = function() { 
     var hello = $stateParams.name; 
     var hello2 = $stateParams.description; 

    }; 

    load(); 

}]); 

我打電話,像這樣的狀態:

$state.go('drillhole.ddhinttype', { name: tab.params.name, description: tab.params.description }); 

在我的控制器名稱和說明屬性始終爲空。

不知道我在這裏錯過了什麼。有任何想法嗎?

+0

您是否通過調試器驗證過「tab.params.name」和「tab.params.description」是否爲空? –

+0

是的,我確實檢查過。價值在那裏。 – Ian

回答

1

如果你把你的網址PARAMS,你將能夠使用訪問它的控制器$stateParams

.state('drillhole.ddhinttype', { 
     url: '/ddhinttype/:name/:description', 
     templateUrl: VIRTUAL_DIR_PATH + '/App/Views/drillholemanager/drillhole/tabddhinttype.html?v=' + fileVer, 
     controller: 'DrillHoleDdhIntTypeController', 
     params: { name: null, description: null } 
    }) 

您可以在這裏閱讀更多關於url路由:https://github.com/angular-ui/ui-router/wiki/url-routing

+0

這看起來就像它要走的樣子。現在我唯一需要弄清的是如何在動態生成的選項卡上設置ui-sref屬性。 – Ian

0

的狀態定義試試這個:

params: { name: undefined, description: undefined }

或本:

params: ['name', 'description']

0

我覺得我應該發佈最終結果。我已決定在URL中傳遞該參數,以便我可以重複使用同一個控制器的多個選項卡,每個選項卡具有相同的功能,但針對數據庫中的不同表格。

這是我的基本控制器的其產生的標籤(CoreLogController.js)的部分:

service.getDrillHoleIntervalTypes() 
     .success(function (res) { 
      $scope.data.drillHoleIntervalTypes = res; 

      for (var i = 0; i < $scope.data.drillHoleIntervalTypes.length; i++) { 
       // add the tab and set it as active if we're in the correct $state 
       $scope.dynamictabs.push({ heading: $scope.data.drillHoleIntervalTypes[i].Name, route: 'drillhole.ddhinttype', params: { ddhinttype: $scope.data.drillHoleIntervalTypes[i].Name }, active: ($scope.$state.params.ddhinttype == $scope.data.drillHoleIntervalTypes[i].Name) }); 
      } 
     }) 
     .error(function (error) { 
      uihelper.showError(error); 
     }); 

而這裏是有關HTML部分,其中突出部被示出(corelog.html):

 <tabset> 
      <tab ng-repeat="t in statictabs" heading="{{t.heading}}" ui-sref="{{t.route}}" active="t.active"></tab> 
      <tab ng-repeat="t in dynamictabs" heading="{{t.heading}}" ui-sref="drillhole.ddhinttype({ddhinttype: '{{t.params.ddhinttype}}'})" active="t.active"></tab> 
     </tabset> 

而且這裏是我定義的狀態(app.js):

.state('drillhole.ddhinttype', { 
     url: '/ddhinttype/{ddhinttype}', 
     templateUrl: VIRTUAL_DIR_PATH + '/App/Views/drillholemanager/drillhole/tabddhinttype.html?v=' + fileVer, 
     controller: 'DrillHoleDdhIntTypeController', 
     params: { ddhinttype: null } 
    }) 

我現在可以訪問每個控制器實例(DrillHoleDdhIntTypeController.js)上的ddhinttype變量告訴它要對哪個表執行操作。

由於ddhinttype也包含在URL中,所以用戶可以創建一個書籤,即使它們是動態生成的,也會將它們帶回到同一個標籤。