search

Home  >  Q&A  >  body text

angular.js - angular 如何获取标签里的文本?

比如代码如下:

<body ng-app="app">
    <p ng-controller="myCtr">
        <p>这是要获取的内容</p>
    </p>
</body>

其中,p标签里的文本,是通过php传过来的。我试过ng-bindng-model获取,结果是undefined,为什么不行?
我要怎么通过angular去获取这个p标签的内容呢 ?
不想引用jq 通过js 去获取。

曾经蜡笔没有小新曾经蜡笔没有小新2744 days ago823

reply all(3)I'll reply

  • 为情所困

    为情所困2017-05-15 17:06:40

    What angularjs recommends is to use the controller to obtain the data through $http, and then render it to the template. If you directly use PHP to output to the template, what else does angularjs do?
    angularjs has jqLite, you can use angular.element(document.querySelector('p')).text() to achieve your effect, but this is not essentially different from using jQuery.

    reply
    0
  • phpcn_u1582

    phpcn_u15822017-05-15 17:06:40

    If you use ng-bind or ng-model only to bind a variable under a scope to it, it will change the value in the tag to undefined. angular.element("#element-id") seems to work

    reply
    0
  • 淡淡烟草味

    淡淡烟草味2017-05-15 17:06:40

    It should be like this

    <body ng-app="app">
        <p ng-controller="myCtr">
            <p ng-repeat="item in lists">{{iteam.name}}</p>
        </p>
    </body>
    
    在控制器中
    angular.module("app",[]);
    app.controller("myCtrl",['$scope','$http'],function($scope,$http){
        $http.get("url").then(function(data){
        $scope.lists=data;
        })
    })

    Get the data transmitted from PHP through the $http service, and then bind it to the view. What I write here is very simple. Generally, the encapsulated interface will be called in the controller, and the specific implementation is usually in the service.

    reply
    0
  • Cancelreply