I have the following html:
enter image description here
I am trying to get the scope like:
console.log("app element scope: " + $('#angularHeaderDiv').scope());
but I received: undefined.
I am angularJS beginner and I simply dont' get why this doesn't work.
UPDATE: This doesn't work either:
var appElement = document.getElementById('angularHeaderDiv');
console.log("app element scope: " + angular.element(appElement).scope());
UPDATE 2: All the code where I try to print out the scope in console:
angular.module('cmApp', [ 'pascalprecht.translate' ])
.config(function($translateProvider) {
$translateProvider.useStaticFilesLoader({
prefix : '/resources/angular/scripts/languages/locale-',
suffix : '.json'
});
// add translation table
$translateProvider.preferredLanguage('en');
var appElement = document.getElementById('angularHeaderDiv');
console.log("app element: " + angular.element(appElement).scope());
});
3 Answers 3
If you have this in your code:
$compileProvider.debugInfoEnabled(false);
...then scope() and isolateScope() will return undefined. If it's set to false, you'll notice other things like ng-scope and ng-isolate-scope disappearing out of class= attributes on elements.
See: https://code.angularjs.org/1.3.16/docs/guide/production#disabling-debug-data
2 Comments
I don't think you actually have a scope there. Assuming you meant $scope, it is a environment that is shared between a controller and a portion of your DOM, and it is injected inside of your controller by AngularJS dependency injection mechanism. Besides that, I don't see any association between your '#angularHeaderDiv' and any scope, since this div is not even associated with any controller.
I'm also not very experienced with AngularJS, but actually I've never seen what you are trying to accomplish here.
Edit
Having seen your edit, where you are trying to access the DOM element inside a config, I would say that that is for sure a non-AngularJS way to solve your problem. I suggest you to read more about the framework.
10 Comments
var appElement = document.getElementById('main'); console.log("app element: " + angular.element(appElement).scope()); because main is already having HeaderCtrl bound to it but this doesn't work either.$scope (with the dollar) outside your controller. And, inside your controller, I don't see the need to access DOM elements like you are doing. It seems that you are taking a jQuery-ish approach to a AngularJS problem.If your application has debugInfoEnabled(false), you could try to reload the app with debug info:
angular.reloadWithDebugInfo()
See here: https://docs.angularjs.org/guide/production#disabling-debug-data
.scopebecause you were including Angular.scope()@RafaelEyng is right, do you mean$scope? Take a look here: docs.angularjs.org/guide/scope Or did you create you're own scope method? The reason.scope()is undefined is because it doesn't exist