2016-10-01 94 views
0

enter image description here剛剛接觸到angularjs。我試圖用角服務發佈的數據,但下面的錯誤注入服務到控制器不能正常工作

angular.js其投擲:12520錯誤:[$注射器:unpr]未知提供商:frontendServiceddProvider < - frontendServicedd < - CustSignupCtrl

service.js

app.service('frontendService', function frontendService ($http, $q, $rootScope){ 

     var list = this; 


     list.registerCust = function(data){ 
      var defer = $q.defer(); 

      $http({ 
       url: $rootScope.endPoint, 
       method: "POST", 
       data: data 
      }) 
      .success(function(res){ 
       console.log("Successfull!"); 
       defer.resolve(res); 
      }) 
      .error(function(err, status){ 
       console.log("Failed !"); 
      }) 

      return defer.promise; 
     } 

     return list; 

    }); 

customer_signup.js

app.controller('CustSignupCtrl', ['$scope', '$filter','frontendService', '$http', 'editableOptions', 'editableThemes','notify','notification','$appConstants', 
    function('$scope', '$filter','frontendService', '$http','editableOptions', 'editableThemes','notify','notification','$appConstants'){ 


    $scope.pw1 = ''; 

    $scope.registerCustomer = function (data) { 
     return frontendService.registerCust(data) 
    } 

    $scope.signupcustomer = function(){ 

     var payload= { 
     first_name: $scope.custForm.fname, 
     last_name: $scope.custForm.lname, 
     phone: $scope.custForm.phone, 
     email:$scope.custForm.email, 
     username:$scope.custForm.username, 
     password:$scope.custForm.pw1, 
     usertype:3 
     } 
     console.log("inside function"); 
     $scope.registerCustomer(payload).then(function(data){ 
     notification.success(data.result); 
     },function(err){ 
     notification.error("Error: contact system admin"); 
     }); 

    } 
    } 
]) 

我給的參考這兩個JS文件index.html中..沒有得到哪裏做錯了..任何人都可以幫助

+0

您的注射不正確。變量應該是類似的 '$ scope','frontendService','$ filter','$ http','editableOptions','editableThemes','notify','notification','$ appConstants'。 您的函數參數在控制器函數 –

回答

2
app.controller('CustSignupCtrl', ['$scope', '$filter', 'frontendService', '$http', 'editableOptions', 'editableThemes','notify','notification','$appConstants', 
    function($scope, $filter, frontendService, $http, editableOptions, editableThemes, notify, notification, $appConstants){ 

    $scope.pw1 = ''; 
}); 

不管你inject到控制器,應以相同的順序。 刪除function內部注射針劑的報價。

+0

中必須是相同的順序謝謝您的回覆..它不工作獲取2錯誤1)customer_signup.js:2未捕獲SyntaxError:意外的字符串2)angular.js:12520錯誤:[ng:areq ]參數'CustSignupCtrl'不是一個函數,沒有定義 – Raghav

+0

你能用瀏覽器控制檯的截圖更新你的問題嗎?那裏應該有一些錯誤。 –

+0

好的..我更新了你現在可以檢查 – Raghav

2

這可能是一個依賴注入不匹配這類問題

AngularJS內噴射物插入功能,因爲它看到他們裏面[]

例如,如果你宣佈你的JS文件中的函數,說喜歡這

var app = angular.module('app',[]); 
app.controller('maincntrl',function($scope){ 

}); 
var search = function($scope,searchtext,searchfilters,searchareas) 
{ 
    return 'result' 
} 

console.log(angular.injector.annotate(search)); 

你將在控制檯中得到的結果將是

["$scope","searchtext","searchfilters","searchareas"] 

AngularJS解析該函數​​的參數作爲陣列

它經過每個陣列元素和它看到"$scope"的瞬間,它注入範圍object成位置

現在您已使用的語法基本上是用於minification

所以要根據你的宣言

app.controller('CustSignupCtrl', ['$scope','frontendService', '$filter','frontendService', '$http', 'editableOptions', 'editableThemes','notify','notification','$appConstants', 
    function('$scope', '$filter','frontendService', '$http','editableOptions', 'editableThemes','notify','notification','$appConstants'){ 

}); 

所以

$scope--> shall be injected into $scope 
frontendService-->shall be injected into $filter 
$filter--> shall be injected into frontendService 
. 
. 
. 
so on 

而且(你在評論中提到)的錯誤發生,因爲您已聲明function參數strings在這種情況下,依賴注入沒有發生。修理這些東西將解決您的問題