Angularjs 获取回调js指令中的返回值
我在Angularjs中创建了一个指令,我需要在其中使用Angularjs 获取回调js指令中的返回值,angularjs,callback,angularjs-directive,Angularjs,Callback,Angularjs Directive,我在Angularjs中创建了一个指令,我需要在其中使用callBackMethod,以便调用控制器的函数 控制器的函数被调用。但控制器的函数正在返回一些值。我想在回调函数中获取该值。如何实现 下面是我的指令代码 .directive('abcOption', function($compile) { return { restrict : 'A', template : '<div class="filter-content"></div>',
callBackMethod
,以便调用控制器的函数
控制器的函数被调用。但控制器的函数正在返回一些值。我想在回调函数中获取该值。如何实现
下面是我的指令代码
.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);
};
}
};
});
这是代码的一小部分。控制器函数被调用,但我在指令函数中没有得到返回值。如果我记录scope.callBackMethod(dataId),我将在控制台日志中得到未定义的
如何使用指令中的callBackMethod
获取返回值?从具有隔离作用域的指令内部调用控制器函数时,需要传递一个对象:
HTML
JS
var-app=angular.module('myApp',[]);
函数ctrl($scope){
$scope.getDisplayName=函数(columnName){
返回“abc”;
};
}
app.directive('abcopion',函数($compile,$timeout){
返回{
限制:“A”,
模板:“abc”,
替换:正确,
范围:{
callBackMethod:“&getDisplayName”
},
链接:函数(作用域、元素、属性){
/*向函数发送一个对象*/
log(scope.callBackMethod({columnName:“hurray”}));
}
};
});
来自CodeHater的答案是可行的,但(只是有点)令人困惑。所以我更新了它,让它更容易理解
HTML
<div ng-app="myApp" ng-controller="ctrl">
{{returnVal}}
<div abc-option callback="setDisplayNameFn(mustBeTheSame)"></div>
</div>
{{returnVal}}
JS
var app = angular.module('myApp', []);
function ctrl($scope){
$scope.setDisplayNameFn = function(whatever) {
$scope.returnVal= whatever;
};
}
app.directive('abcOption', function($compile,$timeout) {
return {
restrict : 'A',
template : '<div class="filter-content"><b>directive html<b></div>',
replace : true,
scope : {
callBackMethod:'&callback'
},
link: function(scope,element,attrs){
/* send an object to the function */
console.log(scope.callBackMethod({mustBeTheSame:"value from directive"}));
}
};
});
var-app=angular.module('myApp',[]);
函数ctrl($scope){
$scope.setDisplayNameFn=函数(无论什么){
$scope.returnVal=任意值;
};
}
app.directive('abcopion',函数($compile,$timeout){
返回{
限制:“A”,
模板:“指令html”,
替换:正确,
范围:{
callBackMethod:“&callback”
},
链接:函数(作用域、元素、属性){
/*向函数发送一个对象*/
log(scope.callBackMethod({mustBeTheSame:“来自指令的值”}));
}
};
});
我觉得很奇怪,对象属性名称必须与从标记中调用的变量相同。真的把我甩了。@Canetroben:欢迎你,很高兴到2017年底还有人像我一样使用ng1。干杯
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"}));
}
};
});
<div ng-app="myApp" ng-controller="ctrl">
{{returnVal}}
<div abc-option callback="setDisplayNameFn(mustBeTheSame)"></div>
</div>
var app = angular.module('myApp', []);
function ctrl($scope){
$scope.setDisplayNameFn = function(whatever) {
$scope.returnVal= whatever;
};
}
app.directive('abcOption', function($compile,$timeout) {
return {
restrict : 'A',
template : '<div class="filter-content"><b>directive html<b></div>',
replace : true,
scope : {
callBackMethod:'&callback'
},
link: function(scope,element,attrs){
/* send an object to the function */
console.log(scope.callBackMethod({mustBeTheSame:"value from directive"}));
}
};
});