然而,虽然我已经看到了一些例子,比如这个:Passing Parameters from a Directive to a function和我没有看到任何你传递一个原语的地方,就像一个数字作为指令的参数传递给控制器函数 .
我尝试过多种方法,但还没有想出语法 .
HTML CODE:
SCRIPT.JS
var app = angular.module('app', []);
app.controller("mainCtrl", function($scope) {
$scope.count = 0;
$scope.ctrlFn = function() {
$scope.count = 0;
console.log('In mainCtrl ctrlFn!');
$scope.count += count;
// console.log("count is: " + JSON.stringify($scope.count));
//Call service here
};
})
.directive('myDirective', function() {
return {
restrict: 'E',
scope: {
'count' : '&',
'ctrlFn' : '&'
},
template: "
link: function(scope, element, attributes) {
var count = null;
scope.ctrlFn = scope.ctrlFn({count: count});
//scope.text = scope.fn({ count: 0 });
}
};
});
原语是否可以作为此用例中的参数传入?如果是这样,我在这里错过了什么?
Aftermath:
如果有人在angularjs文档中寻找这种语法: ctrlFn({count: 10}) ,它在自定义指令下提到:
通常需要通过表达式将隔离范围中的数据传递到父作用域,这可以通过将局部变量名称和值的映射传递到表达式包装函数来完成 . 例如,hideDialog函数会在隐藏对话框时显示一条消息 . 这在指令中通过调用close({message:'closing for now'})指定 . 然后在on-close表达式中可以使用局部变量消息 .