Passing arguments to ngChange event call from inside directive

℡╲_俬逩灬. 提交于 2019-12-04 23:45:04
nikolai karadjov

You have to pass the parameter in the callback like so:

callback({parametername: value});

And you have to match the parameter name with the one declared in the HTML

In your case:

callback({$event: val})

If you want to control when your handler for the ng-change is called, I think the easiest way would be to remove the ng-change completely - you can call the controller function directly from your ng-click callback.

I think this achieves your desired functionality:

http://jsfiddle.net/BtrZH/11/

You can capture the event object from the click if required:

ng-click="select(scope, option.value, $event)"

Then you can call the controller function when desired:

function select(scope, val, $event) {
    if (!scope.disabled && scope.selectedValue != val) {
        scope.selectedValue = val;
        scope.model = val;
        scope.$parent.myCallback($event);
    }
}

The following doesn't look good, but would work (it'd create another variable $event and pass it via ng-change):

ng-click="$event = $event" ng-change="myCallback($event)"
Nate Bosscher

To pass in values to your controller call it using an object with keys corresponding to the receiver arguments as defined on your template.

e.g.

element

<my-element change="myFunction(value, id, event)"></my-element>

caller

{
   "restrict" : ...,
   "scope" : {
       "change" : "&"
    },
   "controller" : function($scope){
       this.someEventHandler = function($event){

          // called here
          scope.change({
              "value" : "somevalue",
              "id" : "someid",
              "event" : $event
          });

       }
   }
}

parent controller receiver

$scope.myFunction = function(v, i, e){
   // do stuff
}

REF: Passing arguments to ngChange event call from inside directive

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!