Как создать экземпляр $ scope для модуля angularjs, который имеет несколько контроллеров? В настоящее время я получаю следующее сообщение об ошибке:
Error: [$injector:modulerr] Failed to instantiate module hello due to:
[$injector:modulerr] Failed to instantiate module navigation due to:
[$injector:modulerr] Failed to instantiate module $scope due to:
[$injector:nomod] Module '$scope' is not available! You either misspelled the module name or forgot to load it. If registering a module ensure that you specify the dependencies as the second argument.
Структура приложения выглядит следующим образом:
Модуль hello
определяется как:
'use strict';
/** * Main module of the application. */
angular
.module('hello', ['ngAnimate', 'ngRoute', 'ngTouch', 'auth', 'home',
'secure', 'public1', 'navigation', 'ui.bootstrap' ])
.config(function ($routeProvider, $httpProvider, $locationProvider) {
//a bunch of config stuff like routeProvider, etc.
})
.run(['$cookies', function($cookies) {
//some startup stuff like setting default values for cookies, etc.
}]);
navigation.js
определяется как:
'use strict';
angular
.module('navigation', ['$scope', 'auth', 'modalService', 'ngRoute', 'ngAnimate', 'ui.bootstrap'])
.controller('navigation', function($scope, auth, modalService, $route, $uibModal) {
// code for various stuff,
// including code calling the second controller below
$scope.someVariable = 'some value';
});
// Please note that $uibModalInstance represents a modal window (instance) dependency.
// It is not the same as the $uibModal service used above.
angular.module('navigation', ['$scope', '$uibModalInstance', 'items'])
.controller('ModalInstanceCtrl', function ($scope, $uibModalInstance, items) {
//code that is controlled by code from the preceding controller
$scope.someOtherVariable = 'some other value';
});
Обратите внимание, что, когда я Ове $scope
из массивов инъекций согласно комментариям ниже, я получаю следующую ошибку в двух строках $scope.someVariable = 'some value'
и $scope.someOtherVariable = 'some other value'
:
'$scope' is not defined.
Только угловые 'module' могут быть введены только внутри массива DI при загрузке приложения. Вы впрыскиваете $ scope зависимость, которая невозможна .. $ scope доступен только внутри контроллер и функция директивной ссылки –
вы не можете вводить $ scope, поскольку это не зависимость. $ scope задается угловым, чтобы его можно было использовать в контроллере. – Rishab777
@PankajParkar Итак, как изменить код в OP, чтобы заставить его работать? Я просто хочу поэкспериментировать с кодом из образца, чтобы я мог перемещать его в другие конфигурации. – CodeMed