2016-09-26 48 views
0

我有問題時,從服務注入依賴關係到控制器。雖然我加入,但的未知供應商factoryprovider < - 工廠< - 控制器角js

Unknown provider: websiteFactoryProvider <- websiteFactory <- listCtrl

還是同樣的錯誤,我基本上需要渲染NG-以我的index.html

的Index.html

<div ng-view> 

</div> 

app.js

var app = angular.module('app', ['ngRoute']); 

app.config(['$routeProvider', function ($routeProvider) { 
    $routeProvider. 

    when('/list', { 
     templateUrl: 'app/views/list.html', controller: 'listCtrl' 
    }). 

    otherwise({ 
     redirectTo: '/list' 
    }); 

}]); 

websiteService.js

app.factory('webiteFactory', ['$http', '$location', function ($http, $location) { 

    var factory = {}; 

    // method that return all websites from an http request 
    factory.getAllWebsites = function() { 
     return $http.get("http://localhost/Replicate/GetAllWebsites"); 
    } 

    //method that returns an object from given array 
    factory.getFilteredObject = function (list, websiteName) { 
     for (i = 0 ; i < list.length ; i++) { 
      if (list[i].Name == websiteName) 
       return list[i]; 
     } 
    } 


    return factory; 
}]); 


/* application services that would not return values */ 
app.service('websiteService', ['$http', function ($http) { 

    //service for pagination 
    this.paginate = function ($scope) { 

     //pagination code 
     $scope.currentPage = 1; 
     $scope.totalItems = $scope.model.length; 
     $scope.numPerPage = 10; 
     $scope.paginate = function (value) { 
      var begin, end, index; 
      begin = ($scope.currentPage - 1) * $scope.numPerPage; 
      end = begin + $scope.numPerPage; 
      index = $scope.model.indexOf(value); 
      return (begin <= index && index < end); 
     }; 


     //ordering code 
     $scope.reverse = true; 
     $scope.order = function (predicate) { 
      $scope.reverse = ($scope.predicate === predicate) ? !$scope.reverse : false; 
      $scope.predicate = predicate; 
     }; 
    } 

    //service to change state of a website 
    this.changeSiteState = function (website) { 
     var newState = website.State == "Stopped" ? "Started" : "Stopped"; 
     $http({ 
      method: 'POST', 
      url: '/Replicate/ChangeState/', 
      data: JSON.stringify({ webName: website.Name, state: newState }), 
      headers: { 'Content-Type': 'application/json' } 
     }).success(function (data) { 
      if (data == "success") { 
       website.State = website.State == "Stopped" ? "Started" : "Stopped"; 
      } 
      else { 
       alert(data); 
      } 

     }).error(function (data, status, headers, config) { 
      alert(data); 
     }); 
    } 
}]) 

listCtrl.js

app.controller('listCtrl', function websiteCtrl($scope, $location, websiteFactory, websiteService, $modal) { 

    //function triggered at the intialization of controller 
    $scope.init = function() { 
     $scope.model = []; 
     setTimeout(function() { 
      websiteFactory.getAllWebsites().success(function (data) { 
       $scope.model = data; 
       websiteService.paginate($scope); 
      }) 
     }, 0); 
    }; 

    //delegation of change state to service method 
    $scope.changeState = function (website) { 
     websiteService.changeSiteState(website); 
    } 

    //open modal and shows details of single object 
    $scope.showDetails = function (websiteName) { 
     var modalInstance = $modal.open({ 
      templateUrl: '../Views/Replicate/Details.html', 
      controller: 'DetailsCtrl', 
      resolve: { 
       obj: function() { 
        return websiteFactory.getFilteredObject($scope.model, websiteName); 
       } 
      } 
     }); 
    } 

}); 
+0

你又寫道的工廠名稱錯誤。 'webiteFactory'!='websiteService' – taguenizy

+0

只是一個錯字;)「webiteFactory」你忘記了一個「S」 –

+0

是的,你是對的@Foubert。感謝您糾正愚蠢的問題。 – Umar

回答

1

你拼錯了「websiteFactory」。在您的工廠定義的代碼是「webiteFactory」,但在控制器,你用不同的名稱中使用「websiteFactory」這就是爲什麼它是不是能找到這個供應商和生產錯誤獲取它: 變化:

app.factory('websiteFactory', ['$http', '$location', function ($http, $location) { 

    var factory = {}; 

    // method that return all websites from an http request 
    factory.getAllWebsites = function() { 
     return $http.get("http://localhost/Replicate/GetAllWebsites"); 
    } 

    //method that returns an object from given array 
    factory.getFilteredObject = function (list, websiteName) { 
     for (i = 0 ; i < list.length ; i++) { 
      if (list[i].Name == websiteName) 
       return list[i]; 
     } 
    } 


    return factory; 
}]); 
-1

當你的工廠未與角度註冊的錯誤出現。將其更改爲

app.factory('websiteFactory', ['$http', '$location', function ($http, $location) { 
    ...do something here... 
} 
+0

以上評論有什麼不同?它是不是已經註冊? – lightstalker89

相關問題