Stéphane GRILLON
Stéphane GRILLON

Reputation: 11862

Use angularjs ng-blur with a call to javascript

I use AngularJs 1.3.4

$scope.change = function (id) {
    console.log("test onChange... " + id);
};

<input ng-change="change({{question.id}})" is OK

but <input ng-blur="change({{question.id}})" have a syntaxe error

Error: [$parse:syntax] Syntax Error: Token '{' invalid key at column 9 of the expression [change({{question.id}})] starting at [{question.id}})].
http://errors.angularjs.org/1.3.4/$parse/syntax?p0=%7B&p1=invalid%20key&p2=9&p3=change(%7B%7Bquestion.id%7D%7D)&p4=%7Bquestion.id%7D%7D)
    at http://localhost:8080/bower_components/angular/angular.js:63:12

Upvotes: 2

Views: 621

Answers (2)

Stas Markov
Stas Markov

Reputation: 11

The difference is the time of parsing of the arguments.

ngBlur directive try to parse {{question.id}} at the compile time. This is an incorrect syntax for angular $parse.

ngChange parses {{question.id}} when event occurs and the value has been already interpolated, that is why you don't call ng-change function with {{question.id}}, at the place of the placeholder the exact value is

Upvotes: 0

tanenbring
tanenbring

Reputation: 780

You shouldn't be using "{{var}}" in the call at all, angular automatically interpolates values in the scope. All you need to do is:

<input ng-blur="change(question.id)" />

My guess is ng-blur and ng-change handle them differently because it's against standard.

Upvotes: 4

Related Questions