最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - get the text of div using angularjs - Stack Overflow

programmeradmin1浏览0评论

i want to get the text of div using angularjs . I have this code

<div ng-click="update()" id="myform.value">Here </div>

where as my controller is something like this

    var  myapp= angular.module("myapp",[]);
    myapp.controller("HelloController",function($scope,$http){

    $scope.myform ={};

    function update()
    {
            // If i have a textbox i can get its value from below alert 
            alert($scope.myform.value);
    }
    });

Can anyone also remand me any good link for angularjs . I dont find angularjs reference as a learning source .

i want to get the text of div using angularjs . I have this code

<div ng-click="update()" id="myform.value">Here </div>

where as my controller is something like this

    var  myapp= angular.module("myapp",[]);
    myapp.controller("HelloController",function($scope,$http){

    $scope.myform ={};

    function update()
    {
            // If i have a textbox i can get its value from below alert 
            alert($scope.myform.value);
    }
    });

Can anyone also remand me any good link for angularjs . I dont find angularjs reference as a learning source .

Share Improve this question asked Aug 29, 2014 at 8:44 UahmedUahmed 1,8455 gold badges30 silver badges46 bronze badges 7
  • 1 My question would be why do you want the content of the div? To do databinding you can use ng-bind, but since there is no "good" other way to change the content of the div with anything else than binding the value of the div is already on the scope. For learning check this out angular codeschool – AirBorne04 Commented Aug 29, 2014 at 8:54
  • Most likely you are trying to do something you should not do at all with Angular. – dfsq Commented Aug 29, 2014 at 8:54
  • well i have like and dislike feature .So if user click on div it will change the text to 'unlike' and update the ng-click function too . So in this way everytime user click i have to change the text of the div – Uahmed Commented Aug 29, 2014 at 8:57
  • 1 Yes but where is that value ming from should be ming from the angular model, like $scope.likeText = 'Like' and the function to be $scope.update = function() { $scope.likeText = 'dislike'; } and in the div you define an attribute ng-bind="likeText" – AirBorne04 Commented Aug 29, 2014 at 9:01
  • yes i am getting values through model and i am getting 1 and 0 from it . so if i am not wrong you are saying me that i use use angularjs variable there as text and just update the value of it right ? – Uahmed Commented Aug 29, 2014 at 9:04
 |  Show 2 more ments

3 Answers 3

Reset to default 4

You should send the click event in the function, your html code should be :

<div ng-click="update($event)" id="myform.value">Here </div>

And your update function should have the event parameter which you'll get the div element from and then get the text from the element like this :

function update(event)
 {
  alert(event.target.innerHTML);
 }

i just thought i put together a proper answer for everybody looking into this question later.

Whenever you do have the desire to change dom elements in angular you need to make a step back and think once more what exactly you want to achieve. Chances are you are doing something wring (unless you are in a link function there you should handle exactly that).

So where is the value ming, it should not e from the dom itself, it should be within your controller and brought into the dom with a ng-bind or {{ }} expression like:

<div>{{ likeText }}</div>

In the controller now you can change the text as needed by doing:

$scope.likeText = 'Like';
$scope.update = function() {
     $scope.likeText = 'dislike'; 
}

For angular tutorials there is a good resource here -> http://angular.codeschool./

Redefine your function as

$scope.update = function() {
 alert($scope.myform.value);
}

A better way to do it would be to use ng-model https://docs.angularjs/api/ng/directive/ngModel

Check the example, these docs can be a bit wordy

发布评论

评论列表(0)

  1. 暂无评论