我有一个指令,用于监听点击事件并获取参数值,但所有3个值都在调试器中显示为param 1的值,其中param 2& 3未定义。
HTML
<div my-directive data-ng-click="(vm.id, vm.title, vm.name)"></div>
指令:
.directive('myDirective', ['config', function (config) {
// ...
link: function (scope, elem, attrs) {
// ...
// click event
elem.bind('click', function () {
attrs.$observe('onClick', function (param1, param2, param3) {
// ...
html中的ng-click
参数值应如何为指令单独拾取的值编写?
答案 0 :(得分:0)
<强> HTML:强>
<div my-directive data-ng-click="ClickMe();"
data-param1="SampleA" data-param2="SampleB" data-param3="SampleC"> </div>
<强>指令:强>
.directive('myDirective', ['config', function (config) {
..............
link: function (scope, elem, attrs) {
..................
// click event
elem.bind('click', function () {
attrs.$observe('onClick', function () {
......
var result1 = attrs.param1;
var result2 = attrs.param2;
var result3 = attrs.param3;
.....
if (result1 != undefined && result2 != undefined && result3 != undefined) {
switch (result1) {
case "SampleA":
// call parent controller on directive's html page
scope.vm.MyCtrl1Function(result1, result2, result3);
break;
.................
}
};
});
}]);