Questions tagged [angularjs-scope]

In AngularJS, a scope is an object that refers to the application model. It is an execution context for expressions.

In AngularJS, a scope is an object that refers to the application model. It is an execution context for expressions. Scopes are arranged in hierarchical structure which mimic the DOM structure of the application. Scopes can watch expressions and propagate events.

All scopes are children of the top level $rootScope using prototypical inheritance. A new child scope is created every time a directive in the DOM is configured to do so.

Common pitfall: using scalar variables on the scope within directives that create a new child scope (such as ng-if, ng-switch, ng-repeat, ng-include, ...) and not being able to reference them, due to the prototypal nature of JavaScript (own copy of variable is created in the child scope).

Useful links

8949 questions
1311
votes
20 answers

How do I access the $scope variable in browser's console using AngularJS?

I would like to access my $scope variable in Chrome's JavaScript console. How do I do that? I can neither see $scope nor the name of my module myapp in the console as variables.
murtaza52
  • 46,887
  • 28
  • 84
  • 120
1126
votes
6 answers

How do I use $scope.$watch and $scope.$apply in AngularJS?

I don't understand how to use $scope.$watch and $scope.$apply. The official documentation isn't helpful. What I don't understand specifically: Are they connected to the DOM? How can I update DOM changes to the model? What is the connection point…
ilyo
  • 35,851
  • 46
  • 106
  • 159
1106
votes
18 answers

What is the difference between '@' and '=' in directive scope in AngularJS?

I've read the AngularJS documentation on the topic carefully, and then fiddled around with a directive. Here's the fiddle. And here are some relevant snippets: From the HTML: {{text}} From the pane…
iwein
  • 25,788
  • 10
  • 70
  • 111
1054
votes
7 answers

'this' vs $scope in AngularJS controllers

In the "Create Components" section of AngularJS's homepage, there is this example: controller: function($scope, $element) { var panes = $scope.panes = []; $scope.select = function(pane) { angular.forEach(panes, function(pane) { …
Alexei Boronine
  • 11,061
  • 4
  • 19
  • 14
866
votes
28 answers

AngularJS : Prevent error $digest already in progress when calling $scope.$apply()

I'm finding that I need to update my page to my scope manually more and more since building an application in angular. The only way I know of to do this is to call $apply() from the scope of my controllers and directives. The problem with this is…
Lightbulb1
  • 12,882
  • 6
  • 22
  • 23
393
votes
8 answers

AngularJS access parent scope from child controller

I've set up my controllers using data-ng-controller="xyzController as vm" I have a scenario with parent / child nested controllers. I have no problem accessing parent properties in the nested html by using $parent.vm.property, but I cannot figure…
zpydee
  • 4,487
  • 4
  • 16
  • 19
362
votes
6 answers

$rootScope.$broadcast vs. $scope.$emit

Now that the performance difference between $broadcast and $emit has been eliminated, is there any reason to prefer $scope.$emit to $rootScope.$broadcast? They are different, yes. $emit is restricted to the scope hierarchy (upwards) - this may be…
New Dev
  • 48,427
  • 12
  • 87
  • 129
324
votes
8 answers

$watch an object

I want to watch for changes in a dictionary, but for some reason watch callback is not called. Here is a controller that I use: function MyController($scope) { $scope.form = { name: 'my name', surname: 'surname' } …
Vladimir Sidorenko
  • 4,265
  • 3
  • 19
  • 13
292
votes
7 answers

How to set the id attribute of a HTML element dynamically with angularjs (1.x)?

Provided an HTML element of type div, how to set the value of its id attribute, which is the concatenation of a scope variable and a string ?
Th. Ma.
  • 9,432
  • 5
  • 31
  • 46
266
votes
5 answers

When writing a directive in AngularJS, how do I decide if I need no new scope, a new child scope, or a new isolated scope?

I'm looking for some guidelines that one can use to help determine which type of scope to use when writing a new directive. Ideally, I'd like something similar to a flowchart that walks me through a bunch of questions and out pops the correct answer…
Mark Rajcok
  • 362,217
  • 114
  • 495
  • 492
219
votes
8 answers

How do I use $rootScope in Angular to store variables?

How do I use $rootScope to store variables in a controller I want to later access in another controller? For example: angular.module('myApp').controller('myCtrl', function($scope) { var a = //something in the scope //put it in the root…
trysis
  • 8,086
  • 17
  • 51
  • 80
187
votes
5 answers

How do I ignore the initial load when watching model changes in AngularJS?

I have a web page that serves as the editor for a single entity, which sits as a deep graph in the $scope.fieldcontainer property. After I get a response from my REST API (via $resource), I add a watch to 'fieldcontainer'. I am using this watch to…
Kevin Hoffman
  • 5,154
  • 4
  • 31
  • 33
182
votes
4 answers

Losing scope when using ng-include

I have this module routes: var mainModule = angular.module('lpConnect', []). config(['$routeProvider', function ($routeProvider) { $routeProvider. when('/home', {template:'views/home.html', controller:HomeCtrl}). …
175
votes
6 answers

Use of symbols '@', '&', '=' and '>' in custom directive's scope binding: AngularJS

I have read a lot about the use of these symbols in the implementation of custom directives in AngularJS but the concept is still not clear to me. What does it mean if I use one of the scope values in the custom directive? var mainApp =…
Ram
  • 3,887
  • 4
  • 27
  • 49
170
votes
5 answers

Can an angular directive pass arguments to functions in expressions specified in the directive's attributes?

I have a form directive that uses a specified callback attribute with an isolate scope: scope: { callback: '&' } It sits inside an ng-repeat so the expression I pass in includes the id of the object as an argument to the callback…
Ed_
  • 18,798
  • 8
  • 45
  • 71
1
2 3
99 100