AngularJS:$ routeProvider完成请求后访问`$ scope`值

时间:2015-09-05 16:35:05

标签: javascript jquery ajax angularjs

如何在完成Ajax请求后调用/访问Controller方法($ scope.some_function())!

var app = angular.module('home', ['ngRoute']);
app.controller('IModalCtrl', function ($scope, $http, $route, $routeParams, $location) {
    $scope.will_call_this = function() {
        //
    };
});

app.config(['$routeProvider', '$locationProvider', '$httpProvider',
    function ($routeProvider, $locationProvider, $httpProvider) {
        $locationProvider.html5Mode(true);
        $routeProvider
            .when('/news/:cat/:slug/:id', {
                templateUrl: function (attr) {
                    return '/news/' + attr.cat + '/' + attr.slug + '/' + attr.id + '?ajax=1'
                },
                controller: 'IModalCtrl'
                // IS ANY PROPERTY/METHOD HERE
                // WHICH WILL ALLOW/ACCESS TO CONTROLLER PROPERTY
            })
            .otherwise('/');
    }]);

在上面,我想在IModalCtrl完成请求后调用$scope.will_call_this()的{​​{1}}函数。就像jQuery ajax一样。

jQuery Ajax例如:

$routeProvider

1 个答案:

答案 0 :(得分:0)

请尝试以下操作。

app.controller('IModalCtrl', function ($scope,$rootScope, $http, $route, $routeParams, $location) {

    $scope.will_call_this = function() {
        //
    };

    $rootScope.$on('$routeChangeSuccess', function (event, next, current) {
        $scope.will_call_this();
    });
});