0

I need an event that executes when the user press enter on the element and there is an answer to that question already. But the situation here is that the only thing that I can pass to the callback is the $event object but I want to pass the $scope data to the callback just like ng-click does:

<input type="text" ng-enter="application.search(application.$hass)"/>

And in the controller I would have:

$scope.application = {
   $hass: "444331"
}

$scope.application.search = function($hass)
{
     alert($hass)// prints 444331
}

How can I achieve it.

br.julien
  • 3,420
  • 2
  • 23
  • 44
Misters
  • 1,337
  • 2
  • 16
  • 29
  • make it as value and inject it to the controller angular.value('hass',444331); – Karthick Kumar Jan 29 '16 at 15:34
  • @KarthickKumar do you have any link of an example that i can see? – Misters Jan 29 '16 at 15:38
  • https://docs.angularjs.org/guide/providers please check this link – Karthick Kumar Jan 29 '16 at 15:39
  • @KarthickKumar thank! But remember that the $scope.application.$hass, it can change if i click over another bottom. In the example you gave me, is constant. Sorry i didnt mention that. – Misters Jan 29 '16 at 15:43
  • unless you copied that directive incorrectly, this actually works; there is no reason that you would be limited to `$event`. – Claies Jan 29 '16 at 15:45
  • that being said, the directive from that answer was written in 2013 for a different version of angular, and has issues with newer releases of angular, in particular, it throws `$apply` already in progress. – Claies Jan 29 '16 at 15:48
  • the value does come through though, so it doesn't even appear like you tested this before asking why it wouldn't work, since I copied literally your code and the directive from the other answer to a working plunker: http://plnkr.co/edit/VSWx30xxeCCi9Qr2njm6?p=preview – Claies Jan 29 '16 at 15:49
  • 2
    also, don't prefix your directives with `ng`, that is a flag that it is a directive written into the core angular framework; when you ask about your own custom directives but use `ng`, you only confuse people, especially since there is an angular class `.ng-enter` – Claies Jan 29 '16 at 15:51
  • @Claies the truth is that i didnot test it, I guess that it was because i assume that it will not work. I tested now and it did worked. I have to thank you for correcting me. I'll try harder the next time. Thanks for your help!! – Misters Jan 29 '16 at 16:00
  • I still don't think that directive is appropriate for use in current angular versions, but that's a topic for another question. – Claies Jan 29 '16 at 16:02
  • I believe that the `this` object is the current `$scope` object, so `search(this, $hass)` should pass the appropriate scope object to the function. – David R Tribble Jan 29 '16 at 16:54

0 Answers0