Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ajax/6.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 ng在修改源变量时重复不反映_Javascript_Ajax_Angularjs - Fatal编程技术网

Javascript ng在修改源变量时重复不反映

Javascript ng在修改源变量时重复不反映,javascript,ajax,angularjs,Javascript,Ajax,Angularjs,我有以下angularJs代码。当源数据更改时,我的ng repeat不会更新我的视图。我查看了其他帖子并添加了$scope.$apply(),$scope.$digest()在我的ajax成功回调结束时,但它没有帮助。其思想是页面的开头将有一个空表,在ajax调用onReady()之后,它将用数据填充行。有人能告诉我我在这里缺少什么,或者有更好的方法来实现同样的目标吗 JS: (function() { var app = angular.module("jmeter-module",

我有以下angularJs代码。当源数据更改时,我的ng repeat不会更新我的视图。我查看了其他帖子并添加了
$scope.$apply()
$scope.$digest()在我的ajax成功回调结束时,但它没有帮助。其思想是页面的开头将有一个空表,在ajax调用onReady()之后,它将用数据填充行。有人能告诉我我在这里缺少什么,或者有更好的方法来实现同样的目标吗

JS:

(function() {
    var app = angular.module("jmeter-module", []);
    app.controller('JmeterTableController', ['$scope', function($scope){
        $scope.data = [];
        $(document).ready(function(){
            var url = "jmeterTableData.html";
            fetchTableData(url, 10, 25);
        });

        function fetchTableData(url, minAge,  maxAge){
            $.ajax({
                type: "GET",
                url: url,
                data: { minAge : minAge,
                    maxAge : maxAge },
                datatype: "json",
                success: function(data){
                    /*If I try to print data here, I see the values rightly getting returned*/
                    $scope.data = data;
                },
                error: function(e){
                    console.log(e);
                }
            });
        }
    }]);

})();
JSP:


.
.
.
{{val.firstName}
{{val.lastResult}

问题在于$.ajax的执行超出了Angular的摘要周期的范围。使用$scope.$apply可以实现这一点,但由于您已经在使用AngularJS xhr帮助器方法,因此最好使用AngularJS xhr帮助器方法:

$http


除非您有很好的理由通过ajax执行简单的http请求,否则这是正确的答案=)还有一件事,如果您确实需要在document ready上调用函数,您还可以切换到使用angular.element(document).ready()函数,但所提供的示例代码不需要以这种方式包装。只需定义你的应用程序和控制器,并让Angular在知道文档准备就绪时启动。例如
<div id="buildGroupsParentDivId" ng-controller="JmeterTableController as row">
.
.
.
<tbody id="jmeter-table-content"    >
            <tr ng-repeat="val in row.data">
                  <td><img title="History" src="/images/history.png" width="20" height="20"></td>
                  <td><input type="checkbox" value="save"></td>
                  <td>{{val.firstName}}</td>
                  <td>{{val.lastResult}}</td>                      
                </tr>
</tbody>
// Simple GET request example :
$http.get('/someUrl').
  then(function(response) {
    // this callback will be called asynchronously
    // when the response is available
  }, function(response) {
    // called asynchronously if an error occurs
    // or server returns response with an error status.
  });