0

コントローラーからディレクティブにメソッドを渡そうとしています。これが私がやっている方法です:

コントローラーで:

$scope.getEmployees = function()
{
    return {2: 'Jane', 3: 'Bob', 4: 'Smith'};
}

$scope.getSelected = function()
{
    return 3;
}

ビュー/HTML で:

<mydirective data-placeHolder="Choose an employee.." 
       data-getOpts="getEmployees()" data-getSelected="getSelected()" />

ディレクティブで:

var dir = 
{
   restrict: 'E',
   templateUrl : 'views/mydirective.html',
   scope: {
       placeholder: '@',
       getSelected: '&',
       getOpts: '&'
   },   
   controller: ['$scope', '$element', '$attrs', 
       function($scope, $element, $attrs)
       {
           console.log( $scope.getOpts() );
       }],

};

MyApp.directive('mydirective', function()
 {
     return dir;
 });

ただし、ディレクティブのコントローラーの次の行:

   console.log( $scope.getOpts() );

コントローラーの関数に設定されたオブジェクトまたは関数を返すのではなく、undefined を生成しgetEmployees()ます。

私は何を間違っていますか?

4

1 に答える 1

2

命名規則に関するものです。このようにキャメルケースをダッシュ​​区切りの単語に変換する必要があります

data-get-opts="getEmployees()" 
于 2013-08-22T04:06:09.500 に答える