无法调用范围。$ apply from my code

时间:2015-05-06 18:32:53

标签: angularjs

当我尝试在'activate'函数下面的代码中调用$ scope。$ apply()时,它会抛出一个错误,说$ scope未定义。 有没有办法在'激活'功能中调用它?

(function () {
'use strict';

angular
    .module('app.reports')
    .controller('DeleteReportController', DeleteReportController);

DeleteReportController.$inject = ['logger', '$mdDialog', '$state', 'reportsService',
    'userPreferenceService', '_'];

function DeleteReportController(logger, $mdDialog, $state, reportsService,
    userPreferenceService, _) {
    /*jshint validthis: true */
    var deleteReport = this;
    var currentReport = null;


    activate();

    /**
    * @name activate
    * @memberOf app.reports.DeleteReportController
    * @description
    *   Activate Delete Report controller
    */
    function activate() {
        currentReport = reportsService.getCurrentReport();
        $scope.$apply();//THIS THROWS AN ERROR saying scope is undefined
    }
    }
})();

1 个答案:

答案 0 :(得分:3)

未定义,您必须将其作为提供者注入:

(function () {
'use strict';

angular
    .module('app.reports')
    .controller('DeleteReportController', DeleteReportController);

DeleteReportController.$inject = ['$scope', 'logger', '$mdDialog', '$state', 'reportsService',
    'userPreferenceService', '_'];

function DeleteReportController($scope, logger, $mdDialog, $state, reportsService,
    userPreferenceService, _) {
    /*jshint validthis: true */
    var deleteReport = this;
    var currentReport = null;


    activate();

    /**
    * @name activate
    * @memberOf app.reports.DeleteReportController
    * @description
    *   Activate Delete Report controller
    */
    function activate() {
        currentReport = reportsService.getCurrentReport();
        $scope.$apply();//THIS THROWS AN ERROR saying scope is undefined
    }
    }
})();