コントローラーからディレクティブにメソッドを渡そうとしています。これが私がやっている方法です:
コントローラーで:
$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()
ます。
私は何を間違っていますか?