Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/361.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript jquery方法未从角度回调调用?_Javascript_Jquery_Angularjs - Fatal编程技术网

Javascript jquery方法未从角度回调调用?

Javascript jquery方法未从角度回调调用?,javascript,jquery,angularjs,Javascript,Jquery,Angularjs,我有以下代码: $http({method: 'GET', url: 'api/participants/areyouhuman'}) .success(function(data, status, headers, config) { console.log(data); $('.result').html(data); //$('.result').append('<

我有以下代码:

$http({method: 'GET', url: 'api/participants/areyouhuman'})
            .success(function(data, status, headers, config) {
                console.log(data);
                $('.result').html(data);
                //$('.result').append('<p>Test</p>');

        })
        .error(function(data, status, headers, config) {
            console.log('error');
        });
当成功回调时,执行视图不会与新html一起反映

我有以下html代码:

     <div class="form-group">
            <div class='result'></div>
     </div>


你能告诉我哪里做错了吗。或任何其他方法,如果您有?

更新以下代码片段:

$http({method: 'GET', url: 'api/participants/areyouhuman'})
        .success(function(data, status, headers, config) {
            console.log(data);
            $scope.content = data;
            //$('.result').html(data);
            //$('.result').append('<p>Test</p>');

    })
    .error(function(data, status, headers, config) {
        console.log('error');
    });
$http({method:'GET',url:'api/participants/areyouhuman'})
.success(函数(数据、状态、标题、配置){
控制台日志(数据);
$scope.content=数据;
//$('.result').html(数据);
//$('.result').append('Test

'); }) .error(函数(数据、状态、标题、配置){ console.log('error'); });
HTML:


{{content}}

首先,您不应该像Angularjs那样进行DOM操作。由于Angular具有强大的双向绑定,因此您的代码如下所示

// I assume you're calling an ajax request in a controller
$http({method: 'GET', url: 'api/participants/areyouhuman'})
        .success(function(data, status, headers, config) {
            // Have a scope variable within the same scope will help you 
            $scope.dataAjax = data;
    })
    .error(function(data, status, headers, config) {
        console.log('error');
    });
然后在HTML中,您可能需要

<div class="form-group">
    <div class='result'>{{dataAjax}}</div>
 </div>

{{dataAjax}}
如果返回的HTML数据与应用程序有关,也许你会考虑使用$Simult.$Apple()来更新DOM。
  • 为什么在使用AngularJS时要使用jQuery
  • jQuery是否已加载
  • 此代码适用于我:
  • 我建议您更改
    $scope
    ,而不是自己控制DOM。那是angular的工作

  • 你需要改变两件事

  • 不要在控制器内操作DOM元素(这是一种糟糕的做法),而是使用普通角度数据绑定:

    $http({method: 'GET', url: 'api/participants/areyouhuman'})
      .success(function(data, status, headers, config) {
        console.log(data);
        $scope.htmlData = data;
    })
    .error(function(data, status, headers, config) {
        console.log('error');
    });
    
  • 为了呈现外部HTML内容,您需要使用指令:

    <div class="form-group">
      <div class='result' ng-bind-html="htmlData"></div>
    </div>
    
    (不要忘记将
    $sce
    作为依赖项添加到控制器)

    $http({method: 'GET', url: 'api/participants/areyouhuman'})
      .success(function(data, status, headers, config) {
        console.log(data);
        $scope.htmlData = data;
    })
    .error(function(data, status, headers, config) {
        console.log('error');
    });
    
    <div class="form-group">
      <div class='result' ng-bind-html="htmlData"></div>
    </div>
    
    $http({method: 'GET', url: 'api/participants/areyouhuman'})
        .success(function(data, status, headers, config) {
            console.log(data);
            $scope.htmlData = $sce.trustAsHtml(data);
            ...