2016-02-09 71 views
0

我無法使用$ state.go()將一些數據傳遞給stateprovider。這裏是我們一直使用的示例代碼。

$stateProvider.state('socialform', { 
     url: "/socialform?webcontent", 
     templateUrl: "base_template/_Sends.html?", 
     data: { pageTitle: 'Social & Website Publishing' }, 
     resolve: { 
      callPreRenderServices: callPreRenderServices 
     } 
    }); 


$scope.isWebContent = function(status) { 
    if(status) { 
     $state.go('socialform', {webcontent:true}); 
    } 
    else { 
     $state.go('socialform'); 
    }   
}; 

基本上,我們需要做的是爲標題變量傳遞給$ state.go(),因此它將取代PAGETITLE到什麼是傳遞變量的值。

從上面這個代碼:

$stateProvider.state('socialform', { 
     url: "/socialform?webcontent", 
     templateUrl: "base_template/_Sends.html?", 
     data: { pageTitle: title }, 
     resolve: { 
      callPreRenderServices: callPreRenderServices 
     } 
    }); 


$scope.isWebContent = function(status) { 
    if(status) { 
     $state.go('socialform', {webcontent:true, title:"some title"}); 
    } 
    else { 
     $state.go('socialform', {title:"another title"}); 
    }   
}; 
+0

爲什麼你不在你的isWebContent函數中設置的服務中使用一個變量,並在你的路由中解析? – cl3m

回答

0

你可以使用一個服務:

module.service('titleService', function() { 
    this.title = null; 
}); 

// ... inject titleService in the calling controller ... 
$scope.isWebContent = function(status) { 
    if(status) { 
     titleService.title = 'Some Title' 
     $state.go('socialform'); 
    } 
    else { 
     titleService.title = 'Another Title' 
     $state.go('socialform'); 
    }   
}; 

然後,您可以通過自定義數據注入它,或者通過解析功能:

// ... inject before route definition, via dependency injection 
data = { title: titleService.title }; 
$stateProvider.state('socialform', { 
    url: "/socialform?webcontent", 
    templateUrl: "base_template/_Sends.html?", 

    // like this 
    data: data, 

    resolve: { 
     callPreRenderServices: callPreRenderServices 
     // Or you can resolve your title from your service 
     // and use pageTitle in your controller 
     pageTitle: ['titleService', function(titleService) { 
      return titleService.title; 
     }] 
    } 
}); 

您也可以將它作爲$ state參數傳遞:

$stateProvider.state('socialform', { 
    url: "/socialform/:webcontent/:title", 
    // ... 
}); 

// ... 

$state.go('socialform', {webcontent: 'something', title: 'some other thing'}); 
相關問題