2011-06-06 08:50:35 -07:00
|
|
|
@ngdoc overview
|
2013-11-05 22:16:11 -08:00
|
|
|
@name Controllers
|
2014-09-04 16:49:16 +01:00
|
|
|
@sortOrder 220
|
2011-06-06 08:50:35 -07:00
|
|
|
@description
|
|
|
|
|
|
2013-10-11 11:46:59 +01:00
|
|
|
# Understanding Controllers
|
2011-06-06 08:50:35 -07:00
|
|
|
|
2017-01-24 17:23:54 +00:00
|
|
|
In AngularJS, a Controller is defined by a JavaScript **constructor function** that is used to augment the
|
|
|
|
|
{@link scope AngularJS Scope}.
|
2011-06-06 08:50:35 -07:00
|
|
|
|
2018-01-10 12:57:57 +01:00
|
|
|
Controllers can be attached to the DOM in different ways. For each of them, AngularJS will
|
|
|
|
|
instantiate a new Controller object, using the specified Controller's **constructor function**:
|
|
|
|
|
|
|
|
|
|
- the {@link ng.directive:ngController ngController} directive. A new **child scope** will be
|
|
|
|
|
created and made available as an injectable parameter to the Controller's constructor function
|
|
|
|
|
as `$scope`.
|
|
|
|
|
- a route controller in a {@link ngRoute.$routeProvider $route definition}.
|
|
|
|
|
- the controller of a {@link guide/directive regular directive}, or a
|
|
|
|
|
{@link guide/component component directive}.
|
2015-07-13 13:18:23 +01:00
|
|
|
|
|
|
|
|
If the controller has been attached using the `controller as` syntax then the controller instance will
|
2018-01-10 12:57:57 +01:00
|
|
|
be assigned to a property on the scope.
|
2011-06-06 08:50:35 -07:00
|
|
|
|
2014-03-03 12:38:26 -08:00
|
|
|
Use controllers to:
|
2011-06-06 08:50:35 -07:00
|
|
|
|
2013-10-11 11:46:59 +01:00
|
|
|
- Set up the initial state of the `$scope` object.
|
|
|
|
|
- Add behavior to the `$scope` object.
|
2011-06-06 08:50:35 -07:00
|
|
|
|
2014-03-03 12:38:26 -08:00
|
|
|
Do not use controllers to:
|
|
|
|
|
|
|
|
|
|
- Manipulate DOM — Controllers should contain only business logic.
|
2017-01-24 17:23:54 +00:00
|
|
|
Putting any presentation logic into Controllers significantly affects its testability. AngularJS
|
2014-03-03 12:38:26 -08:00
|
|
|
has {@link databinding databinding} for most cases and {@link guide/directive directives} to
|
|
|
|
|
encapsulate manual DOM manipulation.
|
2017-01-24 17:23:54 +00:00
|
|
|
- Format input — Use {@link forms AngularJS form controls} instead.
|
|
|
|
|
- Filter output — Use {@link guide/filter AngularJS filters} instead.
|
|
|
|
|
- Share code or state across controllers — Use {@link services AngularJS
|
2014-03-03 12:38:26 -08:00
|
|
|
services} instead.
|
|
|
|
|
- Manage the life-cycle of other components (for example, to create service instances).
|
|
|
|
|
|
2018-01-10 12:57:57 +01:00
|
|
|
In general, a Controller shouldn't try to do too much. It should contain only the business logic
|
|
|
|
|
needed for a single view.
|
|
|
|
|
|
|
|
|
|
The most common way to keep Controllers slim is by encapsulating work that doesn't belong to
|
|
|
|
|
controllers into services and then using these services in Controllers via dependency injection.
|
|
|
|
|
This is discussed in the {@link di Dependency Injection} and {@link services
|
|
|
|
|
Services} sections of this guide.
|
|
|
|
|
|
|
|
|
|
|
2015-03-13 09:00:08 -07:00
|
|
|
## Setting up the initial state of a `$scope` object
|
2011-06-06 08:50:35 -07:00
|
|
|
|
2017-01-24 17:23:54 +00:00
|
|
|
Typically, when you create an application you need to set up the initial state for the AngularJS
|
2013-10-11 11:46:59 +01:00
|
|
|
`$scope`. You set up the initial state of a scope by attaching properties to the `$scope` object.
|
|
|
|
|
The properties contain the **view model** (the model that will be presented by the view). All the
|
2015-03-13 09:00:08 -07:00
|
|
|
`$scope` properties will be available to the {@link templates template} at the point in the DOM where the Controller
|
2013-10-11 11:46:59 +01:00
|
|
|
is registered.
|
|
|
|
|
|
2014-07-03 12:13:12 +01:00
|
|
|
The following example demonstrates creating a `GreetingController`, which attaches a `greeting`
|
|
|
|
|
property containing the string `'Hola!'` to the `$scope`:
|
2014-06-27 21:18:07 -04:00
|
|
|
|
2014-02-06 14:02:18 +00:00
|
|
|
```js
|
2014-06-27 21:18:07 -04:00
|
|
|
var myApp = angular.module('myApp',[]);
|
|
|
|
|
|
|
|
|
|
myApp.controller('GreetingController', ['$scope', function($scope) {
|
2014-03-03 12:38:26 -08:00
|
|
|
$scope.greeting = 'Hola!';
|
2014-06-27 21:18:07 -04:00
|
|
|
}]);
|
2014-02-06 13:33:42 +00:00
|
|
|
```
|
2011-06-06 08:50:35 -07:00
|
|
|
|
2017-01-24 17:23:54 +00:00
|
|
|
We create an {@link module AngularJS Module}, `myApp`, for our application. Then we add the controller's
|
2014-07-03 12:13:12 +01:00
|
|
|
constructor function to the module using the `.controller()` method. This keeps the controller's
|
|
|
|
|
constructor function out of the global scope.
|
|
|
|
|
|
|
|
|
|
<div class="alert alert-info">
|
|
|
|
|
We have used an **inline injection annotation** to explicitly specify the dependency
|
2017-01-24 17:23:54 +00:00
|
|
|
of the Controller on the `$scope` service provided by AngularJS. See the guide on
|
2014-07-03 12:13:12 +01:00
|
|
|
{@link guide/di Dependency Injection} for more information.
|
|
|
|
|
</div>
|
2014-06-27 21:18:07 -04:00
|
|
|
|
2014-07-03 12:13:12 +01:00
|
|
|
We attach our controller to the DOM using the `ng-controller` directive. The `greeting` property can
|
|
|
|
|
now be data-bound to the template:
|
2011-06-06 08:50:35 -07:00
|
|
|
|
2014-02-06 14:02:18 +00:00
|
|
|
```js
|
2014-03-03 12:38:26 -08:00
|
|
|
<div ng-controller="GreetingController">
|
|
|
|
|
{{ greeting }}
|
|
|
|
|
</div>
|
2014-02-06 13:33:42 +00:00
|
|
|
```
|
2013-10-11 11:46:59 +01:00
|
|
|
|
2012-09-26 15:30:55 +02:00
|
|
|
|
2015-03-13 09:00:08 -07:00
|
|
|
## Adding Behavior to a Scope Object
|
2011-06-06 08:50:35 -07:00
|
|
|
|
2013-10-11 11:46:59 +01:00
|
|
|
In order to react to events or execute computation in the view we must provide behavior to the
|
2013-10-28 23:56:45 -07:00
|
|
|
scope. We add behavior to the scope by attaching methods to the `$scope` object. These methods are
|
2013-10-11 11:46:59 +01:00
|
|
|
then available to be called from the template/view.
|
|
|
|
|
|
2015-02-06 15:02:31 -08:00
|
|
|
The following example uses a Controller to add a method, which doubles a number, to the scope:
|
2013-10-11 11:46:59 +01:00
|
|
|
|
2014-02-06 14:02:18 +00:00
|
|
|
```js
|
2014-03-03 12:38:26 -08:00
|
|
|
var myApp = angular.module('myApp',[]);
|
2013-10-11 11:46:59 +01:00
|
|
|
|
2014-03-03 12:38:26 -08:00
|
|
|
myApp.controller('DoubleController', ['$scope', function($scope) {
|
|
|
|
|
$scope.double = function(value) { return value * 2; };
|
|
|
|
|
}]);
|
2014-02-06 13:33:42 +00:00
|
|
|
```
|
2013-10-11 11:46:59 +01:00
|
|
|
|
2017-01-24 17:23:54 +00:00
|
|
|
Once the Controller has been attached to the DOM, the `double` method can be invoked in an AngularJS
|
2013-10-11 11:46:59 +01:00
|
|
|
expression in the template:
|
|
|
|
|
|
2014-02-06 14:02:18 +00:00
|
|
|
```js
|
2014-03-03 12:38:26 -08:00
|
|
|
<div ng-controller="DoubleController">
|
|
|
|
|
Two times <input ng-model="num"> equals {{ double(num) }}
|
|
|
|
|
</div>
|
2014-02-06 13:33:42 +00:00
|
|
|
```
|
2011-06-06 08:50:35 -07:00
|
|
|
|
2013-11-05 22:16:11 -08:00
|
|
|
As discussed in the {@link concepts Concepts} section of this guide, any
|
2013-10-11 11:46:59 +01:00
|
|
|
objects (or primitives) assigned to the scope become model properties. Any methods assigned to
|
2017-01-24 17:23:54 +00:00
|
|
|
the scope are available in the template/view, and can be invoked via AngularJS expressions
|
2014-02-12 22:47:42 +00:00
|
|
|
and `ng` event handler directives (e.g. {@link ng.directive:ngClick ngClick}).
|
2011-06-06 08:50:35 -07:00
|
|
|
|
2013-10-11 11:46:59 +01:00
|
|
|
## Simple Spicy Controller Example
|
2011-06-06 08:50:35 -07:00
|
|
|
|
2017-01-24 17:23:54 +00:00
|
|
|
To illustrate further how Controller components work in AngularJS, let's create a little app with the
|
2011-06-06 08:50:35 -07:00
|
|
|
following components:
|
|
|
|
|
|
2013-11-05 22:16:11 -08:00
|
|
|
- A {@link templates template} with two buttons and a simple message
|
2011-06-06 08:50:35 -07:00
|
|
|
- A model consisting of a string named `spice`
|
2013-10-11 11:46:59 +01:00
|
|
|
- A Controller with two functions that set the value of `spice`
|
2011-06-06 08:50:35 -07:00
|
|
|
|
2015-03-13 09:00:08 -07:00
|
|
|
The message in our template contains a binding to the `spice` model which, by default, is set to the
|
2011-06-06 08:50:35 -07:00
|
|
|
string "very". Depending on which button is clicked, the `spice` model is set to `chili` or
|
|
|
|
|
`jalapeño`, and the message is automatically updated by data-binding.
|
|
|
|
|
|
2016-07-27 18:30:35 +01:00
|
|
|
<example module="spicyApp1" name="controller-spicy-1">
|
2014-02-06 13:33:42 +00:00
|
|
|
<file name="index.html">
|
2014-03-25 17:07:47 -07:00
|
|
|
<div ng-controller="SpicyController">
|
2013-10-11 11:46:59 +01:00
|
|
|
<button ng-click="chiliSpicy()">Chili</button>
|
|
|
|
|
<button ng-click="jalapenoSpicy()">Jalapeño</button>
|
|
|
|
|
<p>The food is {{spice}} spicy!</p>
|
|
|
|
|
</div>
|
2014-02-06 13:33:42 +00:00
|
|
|
</file>
|
|
|
|
|
<file name="app.js">
|
|
|
|
|
var myApp = angular.module('spicyApp1', []);
|
|
|
|
|
|
2014-03-25 17:07:47 -07:00
|
|
|
myApp.controller('SpicyController', ['$scope', function($scope) {
|
2014-02-06 13:33:42 +00:00
|
|
|
$scope.spice = 'very';
|
2014-03-03 12:38:26 -08:00
|
|
|
|
2014-02-06 13:33:42 +00:00
|
|
|
$scope.chiliSpicy = function() {
|
|
|
|
|
$scope.spice = 'chili';
|
|
|
|
|
};
|
2014-03-03 12:38:26 -08:00
|
|
|
|
2014-02-06 13:33:42 +00:00
|
|
|
$scope.jalapenoSpicy = function() {
|
|
|
|
|
$scope.spice = 'jalapeño';
|
|
|
|
|
};
|
|
|
|
|
}]);
|
|
|
|
|
</file>
|
|
|
|
|
</example>
|
2011-06-06 08:50:35 -07:00
|
|
|
|
|
|
|
|
Things to notice in the example above:
|
|
|
|
|
|
2013-10-11 11:46:59 +01:00
|
|
|
- The `ng-controller` directive is used to (implicitly) create a scope for our template, and the
|
2014-03-25 17:07:47 -07:00
|
|
|
scope is augmented (managed) by the `SpicyController` Controller.
|
|
|
|
|
- `SpicyController` is just a plain JavaScript function. As an (optional) naming convention the name
|
2014-08-04 17:28:03 +02:00
|
|
|
starts with capital letter and ends with "Controller".
|
2012-04-10 12:27:16 -04:00
|
|
|
- Assigning a property to `$scope` creates or updates the model.
|
2013-10-11 11:46:59 +01:00
|
|
|
- Controller methods can be created through direct assignment to scope (see the `chiliSpicy` method)
|
2015-07-19 11:33:53 -04:00
|
|
|
- The Controller methods and properties are available in the template (for both the `<div>` element and
|
2014-01-21 13:05:48 +01:00
|
|
|
its children).
|
2012-04-10 12:27:16 -04:00
|
|
|
|
2013-10-11 11:46:59 +01:00
|
|
|
## Spicy Arguments Example
|
2011-06-06 08:50:35 -07:00
|
|
|
|
|
|
|
|
Controller methods can also take arguments, as demonstrated in the following variation of the
|
|
|
|
|
previous example.
|
|
|
|
|
|
2016-07-27 18:30:35 +01:00
|
|
|
<example module="spicyApp2" name="controller-spicy-2">
|
2014-02-06 13:33:42 +00:00
|
|
|
<file name="index.html">
|
2014-03-25 17:07:47 -07:00
|
|
|
<div ng-controller="SpicyController">
|
2013-10-11 11:46:59 +01:00
|
|
|
<input ng-model="customSpice">
|
|
|
|
|
<button ng-click="spicy('chili')">Chili</button>
|
|
|
|
|
<button ng-click="spicy(customSpice)">Custom spice</button>
|
|
|
|
|
<p>The food is {{spice}} spicy!</p>
|
|
|
|
|
</div>
|
2014-02-06 13:33:42 +00:00
|
|
|
</file>
|
|
|
|
|
<file name="app.js">
|
2013-10-11 11:46:59 +01:00
|
|
|
var myApp = angular.module('spicyApp2', []);
|
|
|
|
|
|
2014-03-25 17:07:47 -07:00
|
|
|
myApp.controller('SpicyController', ['$scope', function($scope) {
|
2016-08-10 12:13:14 +02:00
|
|
|
$scope.customSpice = 'wasabi';
|
2013-10-11 11:46:59 +01:00
|
|
|
$scope.spice = 'very';
|
2014-03-03 12:38:26 -08:00
|
|
|
|
2014-03-25 17:07:47 -07:00
|
|
|
$scope.spicy = function(spice) {
|
2013-10-11 11:46:59 +01:00
|
|
|
$scope.spice = spice;
|
|
|
|
|
};
|
|
|
|
|
}]);
|
2014-02-06 13:33:42 +00:00
|
|
|
</file>
|
|
|
|
|
</example>
|
2011-06-06 08:50:35 -07:00
|
|
|
|
2014-03-25 17:07:47 -07:00
|
|
|
Notice that the `SpicyController` Controller now defines just one method called `spicy`, which takes one
|
2013-10-11 11:46:59 +01:00
|
|
|
argument called `spice`. The template then refers to this Controller method and passes in a string
|
2013-11-06 14:02:09 -08:00
|
|
|
constant `'chili'` in the binding for the first button and a model property `customSpice` (bound to an
|
2011-06-06 08:50:35 -07:00
|
|
|
input box) in the second button.
|
|
|
|
|
|
2013-10-11 11:46:59 +01:00
|
|
|
## Scope Inheritance Example
|
|
|
|
|
|
2014-03-03 12:38:26 -08:00
|
|
|
It is common to attach Controllers at different levels of the DOM hierarchy. Since the
|
2014-02-12 22:47:42 +00:00
|
|
|
{@link ng.directive:ngController ng-controller} directive creates a new child scope, we get a
|
2013-10-11 11:46:59 +01:00
|
|
|
hierarchy of scopes that inherit from each other. The `$scope` that each Controller receives will
|
|
|
|
|
have access to properties and methods defined by Controllers higher up the hierarchy.
|
2014-02-06 13:33:42 +00:00
|
|
|
See [Understanding Scopes](https://github.com/angular/angular.js/wiki/Understanding-Scopes) for
|
2013-10-11 11:46:59 +01:00
|
|
|
more information about scope inheritance.
|
|
|
|
|
|
2016-07-27 18:30:35 +01:00
|
|
|
<example module="scopeInheritance" name="controller-scope-inheritance">
|
2014-02-06 13:33:42 +00:00
|
|
|
<file name="index.html">
|
2014-02-13 23:22:02 +00:00
|
|
|
<div class="spicy">
|
2014-03-25 17:07:47 -07:00
|
|
|
<div ng-controller="MainController">
|
2013-10-11 11:46:59 +01:00
|
|
|
<p>Good {{timeOfDay}}, {{name}}!</p>
|
|
|
|
|
|
2014-03-25 17:07:47 -07:00
|
|
|
<div ng-controller="ChildController">
|
2013-10-11 11:46:59 +01:00
|
|
|
<p>Good {{timeOfDay}}, {{name}}!</p>
|
|
|
|
|
|
2014-03-25 17:07:47 -07:00
|
|
|
<div ng-controller="GrandChildController">
|
2013-10-11 11:46:59 +01:00
|
|
|
<p>Good {{timeOfDay}}, {{name}}!</p>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
2014-02-06 13:33:42 +00:00
|
|
|
</file>
|
|
|
|
|
<file name="app.css">
|
|
|
|
|
div.spicy div {
|
|
|
|
|
padding: 10px;
|
|
|
|
|
border: solid 2px blue;
|
|
|
|
|
}
|
|
|
|
|
</file>
|
|
|
|
|
<file name="app.js">
|
2013-10-11 11:46:59 +01:00
|
|
|
var myApp = angular.module('scopeInheritance', []);
|
2014-03-25 17:07:47 -07:00
|
|
|
myApp.controller('MainController', ['$scope', function($scope) {
|
2013-10-11 11:46:59 +01:00
|
|
|
$scope.timeOfDay = 'morning';
|
|
|
|
|
$scope.name = 'Nikki';
|
|
|
|
|
}]);
|
2014-03-25 17:07:47 -07:00
|
|
|
myApp.controller('ChildController', ['$scope', function($scope) {
|
2013-10-11 11:46:59 +01:00
|
|
|
$scope.name = 'Mattie';
|
|
|
|
|
}]);
|
2014-03-25 17:07:47 -07:00
|
|
|
myApp.controller('GrandChildController', ['$scope', function($scope) {
|
2013-10-11 11:46:59 +01:00
|
|
|
$scope.timeOfDay = 'evening';
|
2014-05-08 11:28:16 -04:00
|
|
|
$scope.name = 'Gingerbread Baby';
|
2013-10-11 11:46:59 +01:00
|
|
|
}]);
|
2014-02-06 13:33:42 +00:00
|
|
|
</file>
|
|
|
|
|
</example>
|
2013-10-11 11:46:59 +01:00
|
|
|
|
|
|
|
|
Notice how we nested three `ng-controller` directives in our template. This will result in four
|
|
|
|
|
scopes being created for our view:
|
2011-06-06 08:50:35 -07:00
|
|
|
|
|
|
|
|
- The root scope
|
2014-03-25 17:07:47 -07:00
|
|
|
- The `MainController` scope, which contains `timeOfDay` and `name` properties
|
2016-11-07 20:57:22 -06:00
|
|
|
- The `ChildController` scope, which inherits the `timeOfDay` property but overrides (shadows) the
|
|
|
|
|
`name` property from the previous scope
|
|
|
|
|
- The `GrandChildController` scope, which overrides (shadows) both the `timeOfDay` property defined
|
|
|
|
|
in `MainController` and the `name` property defined in `ChildController`
|
2011-06-06 08:50:35 -07:00
|
|
|
|
2013-10-11 11:46:59 +01:00
|
|
|
Inheritance works with methods in the same way as it does with properties. So in our previous
|
|
|
|
|
examples, all of the properties could be replaced with methods that return string values.
|
2011-06-06 08:50:35 -07:00
|
|
|
|
|
|
|
|
|
2018-01-10 12:57:57 +01:00
|
|
|
## Testing Controllers
|
2011-06-06 08:50:35 -07:00
|
|
|
|
2013-10-11 11:46:59 +01:00
|
|
|
Although there are many ways to test a Controller, one of the best conventions, shown below,
|
2014-02-12 22:47:42 +00:00
|
|
|
involves injecting the {@link ng.$rootScope $rootScope} and {@link ng.$controller $controller}:
|
2011-06-06 08:50:35 -07:00
|
|
|
|
2013-10-11 11:46:59 +01:00
|
|
|
**Controller Definition:**
|
2014-02-06 14:02:18 +00:00
|
|
|
```js
|
2013-10-11 11:46:59 +01:00
|
|
|
var myApp = angular.module('myApp',[]);
|
2011-06-06 08:50:35 -07:00
|
|
|
|
2013-10-11 11:46:59 +01:00
|
|
|
myApp.controller('MyController', function($scope) {
|
|
|
|
|
$scope.spices = [{"name":"pasilla", "spiciness":"mild"},
|
2014-04-21 15:46:53 -07:00
|
|
|
{"name":"jalapeno", "spiciness":"hot hot hot!"},
|
|
|
|
|
{"name":"habanero", "spiciness":"LAVA HOT!!"}];
|
2013-10-11 11:46:59 +01:00
|
|
|
$scope.spice = "habanero";
|
|
|
|
|
});
|
2014-02-06 13:33:42 +00:00
|
|
|
```
|
2011-06-06 08:50:35 -07:00
|
|
|
|
2013-10-11 11:46:59 +01:00
|
|
|
**Controller Test:**
|
2014-02-06 14:02:18 +00:00
|
|
|
```js
|
2011-06-06 08:50:35 -07:00
|
|
|
describe('myController function', function() {
|
|
|
|
|
|
2011-10-07 11:27:49 -07:00
|
|
|
describe('myController', function() {
|
2013-10-11 11:46:59 +01:00
|
|
|
var $scope;
|
|
|
|
|
|
|
|
|
|
beforeEach(module('myApp'));
|
2011-06-06 08:50:35 -07:00
|
|
|
|
2012-03-14 14:34:51 -04:00
|
|
|
beforeEach(inject(function($rootScope, $controller) {
|
2013-10-11 11:46:59 +01:00
|
|
|
$scope = $rootScope.$new();
|
|
|
|
|
$controller('MyController', {$scope: $scope});
|
2012-03-14 14:34:51 -04:00
|
|
|
}));
|
2011-06-06 08:50:35 -07:00
|
|
|
|
|
|
|
|
it('should create "spices" model with 3 spices', function() {
|
2013-10-11 11:46:59 +01:00
|
|
|
expect($scope.spices.length).toBe(3);
|
2011-06-06 08:50:35 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should set the default value of spice', function() {
|
2013-10-11 11:46:59 +01:00
|
|
|
expect($scope.spice).toBe('habanero');
|
2011-06-06 08:50:35 -07:00
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
});
|
2014-02-06 13:33:42 +00:00
|
|
|
```
|
2011-06-06 08:50:35 -07:00
|
|
|
|
2012-03-14 14:34:51 -04:00
|
|
|
|
2015-07-19 11:33:53 -04:00
|
|
|
If you need to test a nested Controller you must create the same scope hierarchy
|
2013-10-11 11:46:59 +01:00
|
|
|
in your test that exists in the DOM:
|
2012-03-14 14:34:51 -04:00
|
|
|
|
2014-02-06 14:02:18 +00:00
|
|
|
```js
|
2012-03-14 14:34:51 -04:00
|
|
|
describe('state', function() {
|
2013-10-24 11:19:28 -07:00
|
|
|
var mainScope, childScope, grandChildScope;
|
2012-03-14 14:34:51 -04:00
|
|
|
|
2013-10-11 11:46:59 +01:00
|
|
|
beforeEach(module('myApp'));
|
|
|
|
|
|
2012-03-14 14:34:51 -04:00
|
|
|
beforeEach(inject(function($rootScope, $controller) {
|
|
|
|
|
mainScope = $rootScope.$new();
|
2014-03-25 17:07:47 -07:00
|
|
|
$controller('MainController', {$scope: mainScope});
|
2012-03-14 14:34:51 -04:00
|
|
|
childScope = mainScope.$new();
|
2014-03-25 17:07:47 -07:00
|
|
|
$controller('ChildController', {$scope: childScope});
|
2013-10-24 11:19:28 -07:00
|
|
|
grandChildScope = childScope.$new();
|
2014-03-25 17:07:47 -07:00
|
|
|
$controller('GrandChildController', {$scope: grandChildScope});
|
2012-03-14 14:34:51 -04:00
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
it('should have over and selected', function() {
|
|
|
|
|
expect(mainScope.timeOfDay).toBe('morning');
|
|
|
|
|
expect(mainScope.name).toBe('Nikki');
|
|
|
|
|
expect(childScope.timeOfDay).toBe('morning');
|
|
|
|
|
expect(childScope.name).toBe('Mattie');
|
2013-10-24 11:19:28 -07:00
|
|
|
expect(grandChildScope.timeOfDay).toBe('evening');
|
2014-12-06 21:45:09 +01:00
|
|
|
expect(grandChildScope.name).toBe('Gingerbread Baby');
|
2012-03-14 14:34:51 -04:00
|
|
|
});
|
|
|
|
|
});
|
2014-02-06 13:33:42 +00:00
|
|
|
```
|