我正在构建一个使用自定义指令的输入过滤的可单击选项列表。Angular Custom Directives:使用链接函数内的参数调用一个函数
HTML:
<combobox
input-model="myModel"
options="myList"
option-label="label"
option-select="selectFn()"></combobox>
的指令(简体):
app.directive("combobox", function() {
return {
restrict: "E",
replace: true,
template: "<input type=‘text’ ng-model=‘inputModel’ />" +
"<button ng-repeat='option in options | " +
"filter: inputModel’" +
"ng-mousedown=‘optionSelected(option)’" +
">{{option[optionLabel]}}</button>",
scope: {
inputModel: "=",
options: "=",
optionLabel: "@",
optionSelect: "&"
},
link: function(scope, elem, attrs) {
scope.optionSelected = function(option) {
// some stuff here...
scope.optionSelect();
}
}
}
})
范围:
$scope.myList = [
{ label: "First Option", value: 1 },
{ label: "Second Option", value: 2 },
{ label: "Second Option", value: 2 }
]
$scope.selectFn() = function() {
// doing stuff here...
}
但我想打电话给selectFn与调用它的选项属性。喜欢的东西:
option-select=“selectFn(option.value)”
或
scope.optionSelect(option);
这可能吗?我可以在范围内调用函数并从链接函数内传递参数吗?
由于定制原因,我不能使用组合框库,如ui-select。