-2

I am stuck with an issue ..

I am using AngularJS and 'have included jQuery in it which is not working in HTML page. Why does this happen?

$('#setting').change(function() {
  alert("hi");
});
<div class="content table-responsive table-full-width">
  <table class="table table-hover table-striped">
    <thead>
      <th>Settings</th>
    </thead>
    <tbody>
      <tr>
        <td>
          <select id="setting" class="form-control">
            <option>---Select---</option>
            <option ng-repeat="item in settings" value="{{item.id}}">{{item.name}}</option>
          </select>
          <div id="btn" class="col-md-12" style="margin-top: 16px;">
            <input type="button" name="Activate" value="Activate" />
            <input type="button" name="Deactivate" value="Deactivate" />
          </div>
        </td>
      </tr>
    </tbody>
  </table>
</div>

Please help me with this. Thanks in advance

Anoop Joshi P
  • 25,373
  • 8
  • 32
  • 53
Pooja Krishna
  • 193
  • 1
  • 5
  • 26

2 Answers2

0

Instead just use ng-change directive:

<select id="setting" class="form-control" ng-change='functionToCall()'>

and in your controller's scope object add this funciton:

$scope.functionToCall = function(){
   alert('hi');
};

If you have started with angularjs then its better to leave the jquery style code. Angularjs is way different than jquery.

This is good starting point."Thinking in AngularJS" if I have a jQuery background?

Community
  • 1
  • 1
Jai
  • 74,255
  • 12
  • 74
  • 103
0

This often happens when you use jQuery against DOM elements which haven't been created yet.

When your webpage loads, the Angular (probably) won't have kicked in, and populated your drop down list yet.

The solution is to use Angular's ng-change instead. Then, your function will get kicked off when you select an item in your drop down list.

I have an example of this here:

Angular tutorial

Here, I update the customer's details whenever the user selects a new customer:

enter image description here

The code, to populate the list, and call a function when the selected item changes, looks like this:

    <select ng-model="selectedCustomer" 
      ng-change="loadOrders();" 
      ng-options="customer as customer.CompanyName for customer in listOfCustomers" >
    </select>
Mike Gledhill
  • 27,846
  • 7
  • 149
  • 159