When you use 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
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:
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!');