Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/25.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 将变量打印到html页面时出现问题_Javascript_Angularjs_Ajax - Fatal编程技术网

Javascript 将变量打印到html页面时出现问题

Javascript 将变量打印到html页面时出现问题,javascript,angularjs,ajax,Javascript,Angularjs,Ajax,我试图在ajax方法中获得用户的名字,并试图在html页面上打印它,但没有打印出来 这是我的html: <div ng-app="app" ng-controller="VolController"> <div class="account-action"> <h3 style="display:inline;"><img class="img" align="left" src="images/u11.png"> <p styl

我试图在ajax方法中获得用户的名字,并试图在html页面上打印它,但没有打印出来

这是我的html:

<div ng-app="app" ng-controller="VolController">
<div class="account-action">

<h3 style="display:inline;"><img class="img"  align="left" src="images/u11.png">
    <p style="color:white">&nbsp;&nbsp; Welcome {{first_name}}<p></h3>  
</div>
问题更新

您应该使用$scope

  $scope.first_name=JSON.stringify($scope.volunteer.VolunteersDTO.first_name);

当您使用
$.ajax
而不是本机
$http
,您应该将分配包装到
$apply

success: function(data) {
    $scope.$apply(function(){
        $scope.volunteer = data;
        $scope.first_name = JSON.stringify($scope.volunteer.VolunteersDTO.first_name);
    });
}

不要使用jquery$.ajax进行ajax调用。请使用angularjs的$http服务进行ajax调用

$http.get(url)


不要忘记在angular中将$http服务添加为dependency。

$scope
缺失,
first\u name
应定义为控制器中的scope变量。比如:
$scope.first\u name=JSON.stringify($scope.autonovate.autonovatesdto.first\u name)尝试过它。。请再看一次qstn。。它仍然不起作用如果您使用Javascript AJAX,您必须让angular知道在angular的上下文之外发生了更改或范围变量已更新。为此,请使用
$scope.$apply
。检查此项:谢谢您的帮助。我们是否必须调用该函数,或者在页面加载时会调用该函数。另外,我们必须将其包含在控制器中的什么位置?无论何时调用非角度异步内容,都应该使用它:例如
$.ajax
而不是
$http
setInterval
而不是
$interval
success: function(data) {
    $scope.$apply(function(){
        $scope.volunteer = data;
        $scope.first_name = JSON.stringify($scope.volunteer.VolunteersDTO.first_name);
    });
}