1

I have an image like:

console.log( $scope.image ); // <img alt="hello" class="form" src="demo.jpg">

I want to have the following output (just the text of alt):

console.log( $scope.image ); // hello

There is an answer for jquery here. But I want to do the same in Angularjs. Any idea?

Edit: Also $scope.image contains more html.

Vahid Najafi
  • 4,654
  • 11
  • 43
  • 88

1 Answers1

3

You can also do the same thing like in your posted answer with jquery using 'angular.element' which give you access to several jquery functionalities set known as jqlite.

var alt = angular.element($scope.image).attr('alt')

EDIT: In case of more html tags inside your $scope.image, do this:

var alt = angular.element($scope.image).find('img').attr('alt')

tomeks
  • 113
  • 5
  • Thanks. But I forgot to say, there is even more `html` beside `img` tag, and when we have more html, it doesn't work. – Vahid Najafi May 27 '17 at 16:36
  • That's not a problem. Wrap all html into angular.element and then call .find('img'). This will give you access to img tag anyway, then call .attr('alt') like in my code example. – tomeks May 27 '17 at 16:44
  • angular.element is jQlite if you don't include jQuery before your angular inclusion. That's why for example you can use .find() only with tag names and not selectors – quirimmo May 27 '17 at 19:18
  • Still getting the same error: `Error: Syntax error, unrecognized expression: aaaaaaa ` – Vahid Najafi May 28 '17 at 06:11