gpt4 book ai didi

javascript - 未知提供商 : $scopeProvider initialling angularjs application

转载 作者:行者123 更新时间:2023-12-03 06:11:05 24 4
gpt4 key购买 nike

我收到此错误未知提供程序:$scopeProvider <- $scope

使用下面的 app.js

(function() {
angular.module('MyApp', ['ngRoute', 'satellizer'])
.config(function($routeProvider, $locationProvider, $authProvider) {
$locationProvider.html5Mode(true);

$routeProvider
.when('/', {
templateUrl: 'partials/home.html'
})
})
.run(function($rootScope, $scope, $window, $location) {
if ($window.localStorage.user) {
$rootScope.currentUser = JSON.parse($window.localStorage.user);
}

$scope.test = 'test';

});
})();

最佳答案

您无法将 $scope 注入(inject)到您的应用程序运行函数中。

You can only get $rootScope injected to services and run function, because each child scope is inherited from its parent scope and the top level scope is rootScope. Since it would be ambigous to inject any scope. Only root scope is provided.

https://stackoverflow.com/a/17371896/3453034

以下是有关运行 block 的一些信息,取自 Angular's module documentation :

Run blocks are the closest thing in Angular to the main method. A run block is the code which needs to run to kickstart the application. It is executed after all of the service have been configured and the injector has been created. Run blocks typically contain code which is hard to unit-test, and for this reason should be declared in isolated modules, so that they can be ignored in the unit-tests.

你可以在你的run方法中设置$rootScope.test = "test",将$rootScope注入(inject)到你的 Controller 中,然后在你的 Controller 中直接通过访问它>$rootScope.

关于javascript - 未知提供商 : $scopeProvider initialling angularjs application,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39316263/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com