2017-05-02 8 views
0

私は角度(とjavascript)を初めて使っています。私はディレクティブを作成しようとしており、ディレクティブのスコープに配列を追加する必要があります。実行時には、私は、文字列を持つか、番号と「セクション」の値を交換した場合、エラーが表示されなくなりanglejs内のスコープに配列を追加するときの型エラー

TypeError: definition.match is not a function 
    at angular.js:7992 
    at forEach (angular.js:417) 
    at parseIsolateBindings (angular.js:7987) 
    at parseDirectiveBindings (angular.js:8028) 
    at addDirective (angular.js:9984) 
    at collectDirectives (angular.js:9142) 
    at compileNodes (angular.js:8974) 
    at compileNodes (angular.js:8990) 
    at compileNodes (angular.js:8990) 
    at compile (angular.js:8859) 

を取得

.directive("startupSections", function(){ 
    return { 
     restrict: "E", 
     transclude: true, 
     scope: { 
      title: "@", 
      sections: [1,2,3] 
     }, 
     link: function(scope, elem, attrs){ 
      console.log (scope); 
     } 

    } 
}); 

:ディレクティブの私のコードは次のようになります。スコープ内のプロパティの値として配列を持つことは可能ですか?はいの場合、どうですか?

答えて

1

このディレクティブのスコープオブジェクトは、スコープ変数をインスタンス化する場所ではありません。スコープオブジェクトは、ディレクティブのスコープ変数を、そのディレクティブが存在するコントローラの変数にマップするはずのキーと値のペアで構成されます。

したがって、 'section'配列にコントローラからのディレクティブに与えなければならない値が含まれている場合は、コントローラで配列を定義し、配列名を属性として渡してディレクティブにバインドし、

scope: { 
     title: "@", 
     sections: "=" 
    }, 

をし、コントローラのようなものが含まれている必要があります:次の構文を使用して、ビュー内のディレクティブにバインドされます

$scope.sectionsArray = [1,2,3] 

を。配列は、ディレクティブにローカルである場合「のセクションでは、」今、あなたのディレクティブ一方

<startup-sections title='SomeTitle' sections="sectionsArray" > 

にスコープのプロパティとしてアクセスできるようになります、あなたはディレクティブのリンクフェーズ内でそれを宣言することができます。

.directive("startupSections", function(){ 
return { 
    restrict: "E", 
    transclude: true, 
    scope: { 
     title: "@" 
    }, 
    link: function(scope, elem, attrs){ 
     scope.sections = [1,2,3] 
     console.log (scope); 
    } 

    } 
}); 

希望します。

関連する問題