我有以下指令
feedBackModule.directive("responseCollection", ['deviceDetector', function (deviceDetector) {
return {
restrict: "E",
templateUrl: 'js/modules/Feedback/directives/feedbackResponse/collection.html',
scope: {
collections: '=',
completeCallback: '&'
},
link: function (scope, element, attr) {
scope.endCollection = function () {
scope.completeCallback(scope.collections);
}
}
};
}]);
该指令(如您所见)采用放置在以下 Controller 中的函数:
feedBackModule.controller('FeedbackResponseController', ['$state', 'Query', 'feedbackSkillService', 'feedbackFactory', 'feedbackResponseService', function ($state, Query, SkillFactory, feedbackFactory, feedbackResponseService) {
var num_users = null;
var user_index = 0;
this.activeUser = null;
this.final = false;
this.feedback = feedbackResponseService.getFeedback();
this.completeUser = function (collections) {
this.activeUser.start = false;
if(user_index < (num_users-1)){
user_index++;
this.activeUser = this.feedback.feedback_has_target_users[user_index];
}
else
{
this.final = true;
}
}
}]);
HTML:
<response-collection complete-callback="frCTRL.completeUser()" collections="frCTRL.feedback.feedback_collections" ng-if="frCTRL.activeUser.start && !frCTRL.final"></response-collection>
该函数可以工作并且被正确调用,但是我用它解析的变量始终是未定义
所以我的问题是如何将变量/对象传递给这样的函数?
最佳答案
将指令参数绑定(bind)更改为 =
:
completeCallback: '='
更改 HTML 以引用该函数(不带 ()
):
complete-callback="frCTRL.completeUser"
你就可以开始了。
关于javascript - Angular 指令将变量解析为函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35957453/