指令侦听具有3个参数的点击事件

时间:2014-05-30 20:54:34

标签: angularjs angularjs-directive

我有一个指令,用于监听点击事件并获取参数值,但所有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参数值应如何为指令单独拾取的值编写?

1 个答案:

答案 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;
                       .................
                  }
              };
          });
      }]);