GetAttribute value with Angular

I have the following controller ... I am trying to get attribute to name

and rating

from HTML input, but I get an errorTypeError: angular.element(...).getAttribute is not a function

app.controller('courseReview', function ($scope, $http) {

  $scope.rateThis = function rateThis(el){
    var elName = angular.element(el).getAttribute('name');
    var rating = angular.element(el).getAttribute('rating');
    document.getElementById(elName+'Rating').value = rating;
  }
});

      

Html

<div ng-controller="courseReview">
  <!-- radio input -->
  <input class="star-5" id="wstar-5" rating="5" type="radio" name="welcome" ng-click="rateThis(this)"/>
</div>

      

Is there any other way to do this?

+3


source to share


1 answer


According to the documentation " https://docs.angularjs.org/api/ng/function/angular.element " to get the attribute of the element you need to use

.attr('name');

      

instead

.getAttribute('name');

      

Attention!

You are passing this

in your html, here this

will not pass the DOM element to the function, rather it will reference the scope. To pass the current element pass$event



HTML change

ng-click="rateThis(this)"

before ng-click="rateThis($event)"

JS Edit

$scope.rateThis = function rateThis($event){
    var elName = angular.element($event.target).attr('name');
    var rating = angular.element($event.target).attr('rating');
    document.getElementById(elName+'Rating').value = rating;
}

      

For reference - http://plnkr.co/edit/ZQlEG33VvE72dOvqwnpy?p=preview

+14


source







All Articles