2016-11-21 36 views
0

我是一名具有5年以上經驗的Web開發人員。現在我學習了平均網站的開發方式,並開始啓動平均堆棧項目。然後我在前端部分有一些問題(角度控制管理)。AngularJS控制器管理平均堆棧開發

我的主要問題是我的角度控制器js文件導致2瀏覽器錯誤。如下所示。

第一誤差如下:

aangular.js:68 Uncaught Error: [$injector:nomod] Module 'app' is not         available! 
You either misspelled the module name or forgot to load it. If registering a module ensure that you specify the dependencies as the second argument.(…)(anonymous function) 
@ angular.js:68(anonymous function) 
@ angular.js:2082ensure 
@ angular.js:2006module 
@ angular.js:2080(anonymous function) 
@ UsersService.js:4(anonymous function) 
@ UsersService.js:111 

第二誤差如下:

angular.js:13920 Error: [ng:areq] Argument 'Users.Controller' is not a function, got undefined 

我的前端代碼結構如下。

  • 控制器 users.controller.js,account.controller.js,projects.controller.js等。
  • 服務 UsersService.js,AccountService.js等
  • app.js(中間件部分)

  • 視圖 的index.html,HTMLS對應於每一控制器JS

    讓我們來仔細查看代碼。

App.js文件:

(function() { 
    angular.module('app', ['ui.router']) 
     .config(config) 
     .run(run); 

    function config($stateProvider, $urlRouterProvider) { 
     // default route 
     $urlRouterProvider.otherwise("/"); 

     $stateProvider 
      .state('home', { 
       url: '/', 
       template: '<h1>Unpasan Admin Page!</h1>', 
       controller: 'Users.Controller', 
       controllerAs: 'vm', 
       //data: { activeTab: 'home' } 
      }) 
      .state('account', { 
       url: '/account', 
       templateUrl: 'views/account.html', 
       controller: 'Accounts.Controller', 
       controllerAs: 'vm', 
       data: { activeTab: 'account' } 
      }) 
      .state('client', { 
      url: '/client', 
      templateUrl: 'views/client.html', 
      controller: 'Clients.Controller', 
      controllerAs: 'vm', 
      data: { activeTab: 'client' } 
      }) 
      .state('user', { 
       url: '/user', 
       templateUrl: 'views/user.html', 
       controller: 'Users.Controller', 
       controllerAs: 'vm', 
       data: { activeTab: 'client' } 
      }) 
      .state('project', { 
       url: '/project', 
       templateUrl: 'views/project.html', 
       controller: 'Projects.Controller', 
       controllerAs: 'vm', 
       data: { activeTab: 'project' } 
      }); 
    } 

UsersService.js:

(function() { 
 

 
    angular 
 
     .module('app') 
 
     .factory('UserService', function(){ 
 

 
       var service = {}; 
 

 
       service.GetCurrent = GetCurrent; 
 
       service.GetAll = GetAll; 
 
       service.GetById = GetById; 
 
       service.GetByUsername = GetByUsername; 
 
       service.Create = Create; 
 
       service.Update = Update; 
 
       service.Delete = Delete; 
 

 
       return service; 
 

 
       function GetCurrent() { 
 
        return $http.get('/api/users/current').then(handleSuccess, handleError); 
 
       } 
 

 
       function GetAll() { 
 
        return $http.get('/api/users').then(handleSuccess, handleError); 
 
       } 
 

 
       function GetById(_id) { 
 
        return $http.get('/api/users/' + _id).then(handleSuccess, handleError); 
 
       } 
 

 
       function GetByUsername(username) { 
 
        return $http.get('/api/users/' + username).then(handleSuccess, handleError); 
 
       } 
 

 
       function Create(user) { 
 
        return $http.post('/api/users', user).then(handleSuccess, handleError); 
 
       } 
 

 
       function Update(user) { 
 
        return $http.put('/api/users/' + user._id, user).then(handleSuccess, handleError); 
 
       } 
 

 
       function Delete(_id) { 
 
        return $http.delete('/api/users/' + _id).then(handleSuccess, handleError); 
 
       } 
 

 
       // private functions 
 

 
       function handleSuccess(res) { 
 
        return res.data; 
 
       } 
 

 
       function handleError(res) { 
 
        return $q.reject(res.data); 
 
       } 
 

 
     }); 
 

 
})();

  • users.controller.js

angular.module('app',[]).controller('UsersController', ['$scope, UserService', function($scope, UserService){ 
 

 
    $scope.users = null; 
 

 
    initController(); 
 
    console.log('Users.Controller:initController'); 
 

 
    function initController() {   // get current user 
 
     UserService.getAllUser().then(function (users) { 
 
      $scope.users = users; 
 
     }); 
 
    } 
 

 
    function saveUser() { 
 
     UserService.Update(vm.user) 
 
      .then(function() { 
 
       FlashService.Success('User updated'); 
 
      }) 
 
      .catch(function (error) { 
 
       FlashService.Error(error); 
 
      }); 
 
    } 
 

 
    function deleteUser() { 
 
     UserService.Delete(vm.user._id) 
 
      .then(function() { 
 
       // log user out 
 
       $window.location = '/login'; 
 
      }) 
 
      .catch(function (error) { 
 
       FlashService.Error(error); 
 
      }); 
 
    } 
 

 

 
}]);

當我點擊index.html中邊欄元素,應當交換 'UI視圖' 標籤的內容。

上面提到的錯誤信息是點擊用戶的'標籤'的情況。

+0

你能解決這個格式嗎? – EJoshuaS

回答

0

而不是controller: 'Users.Controller',它應該是controller: 'UsersController'(請參閱傳遞到'家'的狀態定義的對象)。您應該傳入控制器(UsersController)的名稱,而不是文件名(users.controller)。