2016-07-30 49 views
0

我嘗試使用角度路徑顯示視圖。控制檯不會拋出任何錯誤或警告。不顯示視圖AngularJS

但同樣不顯示視圖。 我在做什麼錯?

的index.html

<!DOCTYPE html> 
<html lang="en" ng-app="app"> 
<head></head> 
    <body> 

     <ng-view></ng-view> 

     <!-- build:js bower/vendor --> 
     <script type="text/javascript" src="../bower_components/angular/angular.min.js"></script> 
     <script src="../bower_components/angular-route/angular-route.min.js"></script> 
     <!-- endbuild --> 

     <script src="./routes.js"></script> 

     <!-- build:app scripts/js || CUSTOMER --> 
     <script src="./components/customer/customerService.js"></script> 
     <script src="./components/customer/customerController.js"></script> 
     <!-- endbuild--> 

    </body> 
</html> 

routes.js

var _templateBase = './components'; 

angular.module('app', [ 
    'ngRoute' 
]) 
.config(['$routeProvider', function ($routeProvider) { 
     $routeProvider 
     .when('/', { 
      templateUrl: _templateBase + '/customer/customer.html' , 
      controller: 'customerController', 
      controllerAs: '_ctrl' 
     }) 
     .otherwise({ 
      redirectTo: '/' 
     }); 
    } 
]); 

costomerService.js

angular.module('app', []) 
     .factory('customerService', function() { 

     }); 

costomerController.js

angular.module('app',[]) 
    .controller('customerController', ['$scope', function($scope, customerService) { 
     // 
    }]); 

這是錯的?因爲視圖不顯示?我使用過時的mertodos,指導我在那裏發佈許多教程。

謝謝,所以他們可以提供幫助。

回答

1

,因爲你已經包括第二個參數(依賴的陣列注入)這個在您的應用:

angular.module('app', [ 
    'ngRoute' 
]) 

您的其他angular.module()定義刪除第二個參數,因爲這是造成一個新的應用程序中創建每一次。

angular.module('app') <-- no second parameter tells it to use the existing 'app' module 
    .factory('customerService', function() { 

    }); 

angular.module('app') <-- no second parameter tells it to use the existing 'app' module 
    .controller('customerController', ['$scope', 'customerService', function($scope, customerService) { 
     // 
    }]); 

我添加customerService您注射陣列控制器定義,因爲數組元素必須完全匹配到你的函數的參數。 此外,如果您正在使用controllerAs語法,就像您在路由定義中所做的那樣,那麼您可能不希望將$scope注入到控制器中。

+0

工程很棒。謝謝。你很棒。 –