Jay*_*tel 8 callback angularjs angularjs-directive
我在Angularjs中创建了一个我需要使用的指令callBackMethod,以便我可以调用Controller的Function.
控制器的函数被调用.但是控制器的函数正在返回一些值.我想在回调函数中获取该值.如何实现?
以下是我的指令代码
.directive('abcOption', function($compile) {
return {
restrict : 'A',
template : '<div class="filter-content"></div>',
replace : true,
scope : {
callBackMethod:'&getDisplayName'
},link: function(scope,element,attrs)
{
scope.getDataName =function(dataId)
{
scope.callBackMethod(dataId);
};
}
};
});
Run Code Online (Sandbox Code Playgroud)
以下代码用于控制器功能
$scope.getDisplayName = function(columnName) {
return 'abc';
};
Run Code Online (Sandbox Code Playgroud)
它是代码的一小部分.控制器函数被调用,但我没有在指令函数中获得返回值.undefined如果我登录scope.callBackMethod(dataId),我将进入控制台日志;
如何callBackMethod在指令中使用返回值?
Alw*_*ner 20
在具有隔离范围的指令内调用控制器的函数时,需要传递一个对象:
<div ng-app="myApp" ng-controller="ctrl">
<div abc-option get-display-name="getDisplayName(columnName)"></div>
</div>
Run Code Online (Sandbox Code Playgroud)
var app = angular.module('myApp', []);
function ctrl($scope){
$scope.getDisplayName = function(columnName) {
return 'abc';
};
}
app.directive('abcOption', function($compile,$timeout) {
return {
restrict : 'A',
template : '<div class="filter-content">abc</div>',
replace : true,
scope : {
callBackMethod:'&getDisplayName'
},
link: function(scope,element,attrs){
/* send an object to the function */
console.log(scope.callBackMethod({columnName:"hurray"}));
}
};
});
Run Code Online (Sandbox Code Playgroud)