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.

1 Answer 1

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')

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks. But I forgot to say, there is even more html beside img tag, and when we have more html, it doesn't work.
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.
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
Still getting the same error: Error: Syntax error, unrecognized expression: aaaaaaa&nbsp;<img alt="😆" class="emojioneemoji" src="./emoji-cdn/2.1.4/assets/png/1f606.png">

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.