2017-05-17 31 views
1

我想知道如果我可以把一個stateprovider放在別的地方,而不是我的app.js,因爲它會導致我在其他頁面出現問題。StateProvider在AngularJS的其他地方比app.js

app.js

var app = angular.module('myApp', 'ui.router']); 

app.config(function ($stateProvider, $urlRouterProvider) { 
    $stateProvider 
     // route to show our basic form (/form) 
     .state('form', { 
      url: '/form', 
      templateUrl: 'templates/step_Form/form.html?v=' + new Date().getDay() 
    }) 

    // url will be nested (/form/profile) 
    .state('form.upload', { 
     url: '/upload', 
     templateUrl: 'templates/step_Form/form-upload.html?v=' + new Date().getDay() 
    }) 

     // url will be /form/interests 
    .state('form.content', { 
     url: '/content', 
     templateUrl: 'templates/step_Form/form-content.html?v=' + new Date().getDay() 
    }) 

$urlRouterProvider.otherwise('/form/upload'); 
}) 

我控制器

angular.module('myApp').controller('ImportGeneralCtrl', function ($scope, $stateProvider, $urlRouterProvider) { 

//myfunctions 

} 

我集成一個多步驟的形式在我的HTML,使用狀態提供。 我怎樣才能擺脫從app.js的狀態提供者只適用於我的控制器?

+0

通常,控制器會隨着狀態或狀態映射到具有控制器的組件。 IIRC,你不能在你的控制器中注入'$ stateProvider'和'$ urlRouterProvider',因爲它們是提供者。 –

回答

1

您不能在控制器中使用$stateProvider$urlRouterProviderproviders),因爲這些providers僅用於配置注入。它可以用在你想要的任何angular.module('myApp').config()中,但你不能在控制器中使用用戶提供者。在控制器,你可以只注入$statemodules,服務,工廠,例如):

angular.module('myApp').controller('myController', function ($scope, $state) {} 

這個小code snippet shows you how to create a Service, a Factory and a Provider

+0

那麼,如何在不將配置文件放入app.js中的情況下使用ui-router的多步驟形式? –

+0

@VincentDucroquet你的問題到底是什麼?你的路由器配置在'app.config()'中應該沒問題。 – lin

+0

在另一頁上,當我的app.js中有.config文件時,出現錯誤: str.replace不是函數 at replaceAll(toolbar.js?v = 636305309810171550&l = fr&u = 636306206076525625:5) –