Re: [AngularJS] HELP! How do I get the text value from "element('a').text()"?

3,568 views
Skip to first unread message
Message has been deleted

Daniel Doyon

unread,
Nov 23, 2012, 1:35:19 PM11/23/12
to ang...@googlegroups.com
Not 100% sure of what you are wanting to do here, below is examples of how you would test your a tag. 

expect(element('a').text()).toMatch(/I WANT THIS TEXT/);

 expect(element('a').attr('href')).toMatch('#/somehref');

or am i missing what you are wanting to do



On Nov 23, 2012, at 6:25 AM, roy...@gmail.com wrote:

In Angular e2e api, the element().text() function doesn't return a string as I'd expect, but instead returns an object (I had to force it to fail in an 'expect' command to figure that out!). How do I get the text of the 'value' element (which is where my text is)?

For example:

var myobj = element('a').text();

... actually populates myobj with this (which you can see if you do - expect(myobj).toBe("X");  - so you get the error message containing what myobj is):

{"name":"element 'a' text","fulfilled":true,"value":"I WANT THIS TEXT"};

However, if I try to get that value I just get 'undefined' (although the other items all work fine). So ...

myobj.name      === "element 'a' text"
myobj.fulfilled === true
myobj.value     === [undefined]

But if I hard code what's returned from the element().text() call like this:

var myobj = {"name":"element 'a' text","fulfilled":true,"value":"I WANT THIS TEXT"};

... then it works:

myobj.name      === "element 'a' text"
myobj.fulfilled === true
myobj.value     === "I WANT THIS TEXT"

What's going on here? Why can't I access the 'value' component of element().text()?

--
You received this message because you are subscribed to the Google Groups "AngularJS" group.
To post to this group, send email to ang...@googlegroups.com.
To unsubscribe from this group, send email to angular+u...@googlegroups.com.
Visit this group at http://groups.google.com/group/angular?hl=en-US.
 
 

roy...@gmail.com

unread,
Nov 23, 2012, 2:39:13 PM11/23/12
to ang...@googlegroups.com, roy...@gmail.com
The object works in 'expect', but can't be assigned to a variable. For example, "var x = element('a').text();" will not put "I WANT THIS TEXT" into the x variable. I want to get that value so I can log it, for example.

It seems to be the same sort of object as is returned by "repeater('a').count()" ... which I also want to put into a variable.

roy...@gmail.com

unread,
Nov 26, 2012, 3:16:29 AM11/26/12
to ang...@googlegroups.com, roy...@gmail.com
Oh dear - this was answered in another post just there. It seems these functions don't return values, they return promises. Wow. Now I need to go and learn all about asynchronous javascript ... or find another way to do this. 

Roy Collings

unread,
Dec 4, 2012, 4:04:45 AM12/4/12
to ang...@googlegroups.com, roy...@gmail.com

Ratnesh Oza

unread,
Dec 12, 2013, 2:24:58 AM12/12/13
to ang...@googlegroups.com, webde...@gmail.com
Hi

We can get text of element by using this in angular js

angular.element(a).text();

please find the example in which i am getting the text and class of selected element.

<!doctype html>
<html ng-app>
<head>
    <title>Hello Angural</title>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.min.js"></script>
    <script>
        function getTextController($scope){
           $scope.getText = function(e){
             e.stopPropagation();
             alert("Selected element text is "+angular.element(e.target).text()+". We can also get attribute value like class='"+ angular.element(e.target).attr("class")+"'");
           }
        }
    </script>
</head>
<body>
    <div ng-controller="getTextController" ng-click="$event.preventDefault();">
            <a href="#" class="home" ng-click="getText($event);">Home</a>
            <a href="#" class="projects" ng-click="getText($event);">Projects</a>
            <a href="#" class="services" ng-click="getText ($event);">Services</a>
            <a href="#" class="contact"  ng-click="getText ($event);">Contact</a>                  
     </div>
</body>
</html>
Reply all
Reply to author
Forward
0 new messages