0

別のJavaスクリプトファイルにserviceディレクティブを使用して単一または複数のモデルを挿入しようとしているときにエラーが発生していますが、単一のJavascriptファイル。ここでは顧客がモデルです単一または複数モデルangularJsの別々のjavascriptファイルでserviceディレクティブを使用してInjecting

エラー:

"Binding.js:33キャッチされないにReferenceError:お客様が定義されていません"。 Binding.js:9キャッチされないにReferenceError:お客様が定義されていないBinding.js @(匿名関数):9 angular.js:13424エラー:[$インジェクター:UNPR]不明プロバイダ:customerProvider < - 顧客< - myCustomerController

以下

は私のコードです:

モデル(customer.js):コード(Binding.js)01バインディング

 function Customer() { 
     this.customerName = "David"; 
     this.customerCode = "C001"; 
     } 

function BindingCode($scope,customer) { 
    $scope.customer = customer; 
    } 
    var myCustomerApp = angular.module("myCustomerApp", []); 
    myCustomerApp.controller("myCustomerController", BindingCode); 
    myCustomerApp.service('customer', Customer); 

コードの表示(customer.html)

<!DOCTYPE html> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<title></title> 
<script src="Scripts/angular.js"></script> 
<script src="Scripts/Binding.js"></script> 
</head> 


<body ng-app="myCustomerApp"> 
    <div ng-controller="myCustomerController"> 

    <table> 

     <tr> 
      <td> 
       CustomerName 
      </td> 
      <td> 
       <input type=text id="CustomerName" ng-model="customer.customerName" /><br /> 

      </td> 
     </tr> 
     <tr> 
      <td> 
       CustomerCode 
      </td> 
      <td> 
       <input type=text id="CustomerCode" ng-model="customer.customerCode" /> 

      </td> 
     </tr> 

    </table> 

</div> 

しかし、同じコードがここでの唯一のモデルとビューを作業してcustomer.htmlにセクションスクリプトに含まれるコードをバインドされています。 モデル:Customer.js

あなたが最初にそのサービスをがyousing前に、コントローラにサービスを注入する必要があり
function Customer() { 
    this.customerName = "David"; 
    this.customerCode = "C001"; 
} 

ビューコード(customer.html)

<!DOCTYPE html> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
    <title></title> 
    <script src="Scripts/angular.js"></script> 
    <script src="Scripts/CustomerModle.js"></script> 
    </head> 

    <script> 

    function BindingCode($scope, customer) { 
     $scope.customer = customer; 

    } 
    var myCustomerApp = angular.module("myCustomerApp", []); 
    myCustomerApp.controller("myCustomerController", BindingCode); 
    myCustomerApp.service('customer', Customer); 

</script> 

<body ng-app="myCustomerApp"> 
    <div ng-controller="myCustomerController"> 
     </div> 

</body> 
</html> 
+0

これはAngular2とどのように関連していますか? –

+0

あなたは 'customer.js'をページに入れましたか? –

+0

私はそれを含める必要があります – sudhir

答えて

-1

BindingCode.$inject = ['Customer'] 

あなたcustomer.htmlファイルに<script src="Scripts/customer.js"></script>を追加

var myCustomerApp = angular.module("myCustomerApp", []); 
myCustomerApp.controller("myCustomerController", BindingCode); 
BindingCode.$inject = ['Customer'] 

function BindingCode($scope,Customer) { 
$scope.customer = Customer; 
} 

Binding.jsを並べ替え

var myCustomerApp = angular.module("myCustomerApp", []); 
myCustomerApp.service('customer', Customer); 
function Customer() { 
    this.customerName = "David"; 
    this.customerCode = "C001"; 
    } 

customer.jsを並べ替え

+0

バインディングコードを見てください、私は以下のコードを使用して注入しました:myCustomerApp.service( 'customer'、Customer); – sudhir

+0

注射していない、ちょうど使用しています。あなたは注入せずに外部サービスを使用することはできません。 myCustomerApp.service( 'customer'、Customer); 'customer'という名前の新しいサービスに 'Customer'という名前を割り当てています。サービスを初期化するだけです。それはコントローラへのリンクがありません –

+0

私はセクション内のcustomer.htmlにbinding.jsコードを移動している場合、それは動作しますが、別のjavascriptファイルに移動した場合 'doesnot work.Also私はモデル(customer.js)コードをbinding.jsに追加して、再び動作します。次に、注入問題がどこにありますか?myCustomerApp.service( 'customer'、function Customer(){ this.customerName = "David"; this.customerCode = "C001"; } ); – sudhir

関連する問題