从AngularJS中的response.data中提取div

时间:2016-03-01 22:29:44

标签: javascript angularjs ajax

我正在尝试从Angular中的ajax响应中提取div。我不想显示所有页面。我做过这样的事情,但注意到了。

$http({
        method: 'GET',
        url: '/angularJS/eg1.html'
    })
        .then(function (response) {
            $scope.page = $sce.trustAsHtml(response.data)
            $scope.result = $scope.page.document.getElementById('ajax');
        });

我的观点是这样的

<div class="container">
<div class="row" ng-bind-html="result">
</div>

我要展示的div:

<div id="ajax">
    <h2>Guess the Number !</h2>
    <p class="well lead">Guess the computer generated random number between 1 and 1000.</p>
    <label>Your Guess: </label><input type="number" ng-model="guess"/>
    <button ng-click="verifyGuess()" class="btn btn-primary btn-sm">Verify</button>
    <button ng-click="initializeGame()" class="btn btn-warning btn-sm">Restart</button>
    <p>
</div>

我是棱角分明的新人,请耐心等待:)。

1 个答案:

答案 0 :(得分:0)

Yo可以使用jqLiteid获取元素。

喜欢这个

var elem = angular.element('<div id='dt'> <span id='ajax'>312312</span></div>');
console.log(elem[0].querySelector('#ajax'));

jsfiddle上的实例。

angular.module('ExampleApp', [])
  .controller('ExampleController', function($scope) {
    $scope.name = "<div id='dt'> <span id='ajax'>312312</span></div>";
    var elem = angular.element($scope.name);
    console.log(elem[0].querySelector('#ajax'));
  });
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="ExampleApp">
  <div ng-controller="ExampleController">
    <form name="testForm">
      <input name="myName" ng-model="name">
    </form>
  </div>
</div>