Sachin Kainth
Sachin Kainth

Reputation: 46750

Call method on Angular scope from Underscore Template

I have an underscore template that is called from an Angular controller. I have a dropdown on the template and a call to onchange on the dropdown. The onchange attempts to calls a method on the scope on the controller. I have tried everything to get the method called in the onchange but doing this

<select onchange="foo(this.value)">

gives me

foo is not defined

and

<select onchange="scope.foo(this.value)">

gives me

scope is not defined

and

<select onchange="$scope.foo(this.value)">

gives me

$scope is not defined

Is it even possible to call a method on the controller in this way?

Upvotes: 0

Views: 330

Answers (2)

Gareth Williams
Gareth Williams

Reputation: 233

In the code that is calling the _.template() function, assuming its putting the templated value into a variable and has access to scope:

Find the element in the templated markup: var select = $(templatedVar).find('#selectId');

Add a change event: select.change(function() { //call scope function });

Upvotes: 2

Hilmi
Hilmi

Reputation: 3441

use ngChange instead attribute of onchange

check this link for more info "https://docs.angularjs.org/api/ng/directive/ngChange"

Upvotes: 0

Related Questions