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。
$scope
,而不是自己控制DOM。那是angular的工作你需要改变两件事
$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>
(不要忘记将$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);
...