aboutsummaryrefslogtreecommitdiffstats
path: root/docs/content/guide/dev_guide.scopes.working_scopes.ngdoc
blob: ab507e16726a47aa3637e3b7615b47430360dfa3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
@workInProgress
@ngdoc overview
@name Developer Guide: Scopes: Working With Angular Scopes
@description


When you use {@link api/angular.directive.ng:autobind ng:autobind} to bootstrap your application,
angular creates the root scope automatically for you. If you need more control over the
bootstrapping process, or if you need to create a root scope for a test, you can do so using the
{@link api/angular.scope angular.scope()} API.


Here is a simple code snippet that demonstrates how to create a scope object, assign model
properties to it, and register listeners to watch for changes to the model properties:


<pre>
var scope = angular.scope();
scope.salutation = 'Hello';
scope.name = 'World';


// Verify that greeting is undefined
expect(scope.greeting).toEqual(undefined);


// Set up the watcher...
scope.$watch('name', function(){
// when 'name' changes, set 'greeting'...
this.greeting = this.salutation + ' ' + this.name + '!';
}
);


// verify that 'greeting' was set...
expect(scope.greeting).toEqual('Hello World!');


// 'name' changed!
scope.name = 'Misko';


// scope.$eval() will propagate the change to listeners
expect(scope.greeting).toEqual('Hello World!');


scope.$eval();
// verify that '$eval' propagated the change
expect(scope.greeting).toEqual('Hello Misko!');
</pre>


## Related Topics


* {@link dev_guide.scopes Angular Scope Objects}
* {@link dev_guide.scopes.understanding_scopes Understanding Scopes}
* {@link dev_guide.scopes.controlling_scopes Applying Controllers to Scopes}
* {@link dev_guide.scopes.updating_scopes Updating Scopes}


## Related API


* {@link api/angular.scope Angular Scope API}