AngularJS在从Ajax加载模型后更新View

Ste*_*ini 9 ajax angularjs

我是angularjs开发的新手,我写了这个简单的应用程序,但是在启动时从ajax请求加载模型后,我不知道如何更新视图!

当我在photos.php中添加延迟时,此代码不起作用,使用:sleep(3); 用于模拟远程服务器延迟!相反,如果search.php是快速的,它的工作!

<!doctype html>
<html ng-app="photoApp">
<head>
<title>Photo Gallery</title>
</head> 
<body>
    <div ng-view></div>

<script src="../angular.min.js"></script>
<script>
'use strict';

var photos = [];  //model

var photoAppModule = angular.module('photoApp', []);

photoAppModule.config(function($routeProvider) {
    $routeProvider.when('/photos', {
                        templateUrl: 'photo-list.html',
                        controller: 'listCtrl' });
    $routeProvider.otherwise({redirectTo: '/photos'});
})
.run(function($http) {
    $http.get('photos.php')//load model with delay
    .success(function(json) {

        photos = json; ///THE PROBLEM HERE!! if photos.php is slow DON'T update the view!

    });
})
.controller('listCtrl', function($scope) {

    $scope.photos = photos;

});
</script>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

输出photos.php

[{"file": "cat.jpg", "description": "my cat in my house"},
 {"file": "house.jpg", "description": "my house"},
 {"file": "sky.jpg", "description": "sky over my house"}]
Run Code Online (Sandbox Code Playgroud)

照片list.html

<ul>
  <li ng-repeat="photo in photos ">
    <a href="#/photos/{{ $index }}">
        <img ng-src="images/thumb/{{photo.file}}" alt="{{photo.description}}" />
    </a>
  </li>
</ul>
Run Code Online (Sandbox Code Playgroud)

编辑1,推迟解决方案:

.run(function($http, $q) {

    var deferred = $q.defer();

    $http.get('photos.php')//load model with delay
    .success(function(json) {
        console.log(json);

        photos = json; ///THE PROBLEM!! if photos.php is slow DON'T update the view!

        deferred.resolve(json);//THE SOLUTION!
    });

    photos = deferred.promise;
})
Run Code Online (Sandbox Code Playgroud)

编辑2,服务解决方案:

... 
//require angular-resource.min.js
angular.module('photoApp.service', ['ngResource']).factory('photoList', function($resource) {
    var Res = $resource('photos.php', {},
        {
            query: {method:'GET', params:{}, isArray:true}
        });
    return Res;
});

var photoAppModule = angular.module('photoApp', ['photoApp.service']);

...

.run(function($http, photoList) {

    photos = photoList.query();
})
...
Run Code Online (Sandbox Code Playgroud)

Ben*_*ull 6

简短的回答是这样的:

.controller('listCtrl', ['$scope', '$timeout', function($scope, $timeout) {
    $timeout(function () {
        $scope.photos = photos;
    }, 0);
}]);
Run Code Online (Sandbox Code Playgroud)

答案很长:请不要像这样混合常规的javascript和角度.重新编写代码,以便angular知道任何时候发生了什么.

var photoAppModule = angular.module('photoApp', []);

photoAppModule.config(function($routeProvider) {
    $routeProvider.when('/photos', {
        templateUrl: 'photo-list.html',
        controller: 'listCtrl' 
    });

    $routeProvider.otherwise({redirectTo: '/photos'});
});

photoAppModule.controller('listCtrl', ['$scope', function($scope) {
    $scope.photos = {};

    $http.get('photos.php') // load model with delay
        .success(function(json) {
            $scope.photos = json; // No more problems
        });
}]);
Run Code Online (Sandbox Code Playgroud)


Ket*_*tan 0

您需要将视图中的元素绑定到$scope对象的属性(简单或对象)。一旦 $scope 对象更新,视图应该自行更新。这就是 AngularJS 的美妙之处。

编辑:请将您的控制器注册为

photoAppModule.controller('listCtrl', function($scope){
  $scope.photos = photos;

});
Run Code Online (Sandbox Code Playgroud)

如果 photos 变量不可用,那么您可能必须使用该变量创建一个服务并注入控制器。