在原始js中,此角度指令起作用:
angular.module("main.vips").directive("confirmModal", function($modal) {
var modalWork = function(modalInstance, $scope) {
return modalInstance.result.then((function() {
return console.log($scope.commentBox.text);
}), function() {
return console.log("Canceled");
})["finally"](function() {
return $scope.commentBox.text = "";
});
};
var _confirmModal = function($scope) {
return $modal.open({
templateUrl: "vips/confirm_modal.html",
scope: $scope,
backdrop: true
});
};
return {
scope: {
iconAttribute: "@"
},
controller: function($scope, $element, $attrs) {
var modalInstance;
modalInstance = void 0;
this.runConfirmModal = function() {
modalInstance = _confirmModal($scope);
}
$scope.commentBox = {};
$scope.cancel = function() {
modalInstance.dismiss();
modalWork(modalInstance, $scope);
};
$scope.ok = function() {
modalInstance.close();
modalWork(modalInstance, $scope);
};
}
};
});
但是在coffescript版本中,它并没有:
angular.module("main.vips").directive "confirmModal", ($modal) ->
modalWork = (modalInstance, $scope) ->
modalInstance.result.then((->
console.log $scope.commentBox.text
), ->
console.log "Canceled"
)["finally"] ->
$scope.commentBox.text = ""
_confirmModal = ($scope) ->
$modal.open
templateUrl: "vips/confirm-modal.html"
scope: $scope
backdrop: true
scope:
iconAttribute: "@"
controller: ($scope, $element, $attrs) ->
modalInstance = undefined
@runConfirmModal = ->
modalInstance = _confirmModal($scope)
$scope.commentBox = {}
$scope.cancel = ->
modalInstance.dismiss()
modalWork modalInstance, $scope
$scope.ok = ->
modalInstance.close()
modalWork modalInstance, $scope
从其他指令调用时:
angular.module("main.vips").directive "deleteButton", ($modal) ->
templateUrl: "vips/directives/delete-button.html"
require: "^confirmModal"
restrict: "AE"
link: (scope, element, attr, ctrl) ->
#do work for this element. When done, call confirm.
element.on "click", (event) ->
ctrl.runConfirmModal()
我明白了:
Uncaught TypeError: Object function () {
modalInstance.close();
return modalWork(modalInstance, $scope);
} has no method 'runConfirmModal'
为什么它没有在咖啡脚本中找到'runConfirmModal'
以及该怎么办?
答案 0 :(得分:1)
看起来controller
函数的返回值设置为ctrl
值。
尝试在控制器函数的末尾(在$ scope.ok之后)添加return
,或者更改控制器以返回api对象(据我所知的文档),如下所示:
controller: ($scope, $element, $attrs) ->
modalInstance = undefined
$scope.commentBox = {}
$scope.cancel = -> ...
$scope.ok = -> ...
runConfirmModal: -> modalInstance = _confirmModal $scope