-1

提前道歉,我只是在学习AngularJS。虽然我可以让它实际工作,但我想知道使用 AJAX 操作填充我的页面加载视图的更好方法是什么:

module.run(['$rootScope', '$location', '$routeParams', '$http', function($rootScope, $location, $routeParams, $http) {
    $rootScope.$on('$routeChangeSuccess', function(e, current, pre) {
    console.log('Current route name: ' + $location.path());
    // Get all URL parameter
    console.log($routeParams);
    $rootScope.currentEditObject = { name: '' };
    if ($routeParams.id) {
        if ($location.path().startsWith('/object')) {
        $rootScope.type = 'object';
        $.ajax({
            type: 'GET',
            url: 'https://localhost/getObject',
            data: {
            objectId: $routeParams.id
            }
        }).success(function(data, status) {
            $rootScope.$apply(function() { $rootScope.currentEditObject = data.response.object; });
        });

        }
    }
    });
}]);

我开始阅读$apply它如何影响摘要,但我怀疑有比这更好的做法。从 AJAX 请求加载模型的“Angular 方式”是什么?

4

1 回答 1

0

I assumed from examples I read that using $http meant something like this:

$http({
    url: '....',
    method: 'GET',
    data: {}
}).success(...

Doing this resolved it:

$http.get(url).success(successFunction)
于 2013-10-19T19:50:09.933 回答