2012-03-08 23:00:38 +00:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @ngdoc directive
|
2012-06-12 06:49:24 +00:00
|
|
|
* @name ng.directive:ngController
|
2012-03-08 23:00:38 +00:00
|
|
|
*
|
|
|
|
|
* @description
|
2012-04-06 23:35:17 +00:00
|
|
|
* The `ngController` directive assigns behavior to a scope. This is a key aspect of how angular
|
2012-03-08 23:00:38 +00:00
|
|
|
* supports the principles behind the Model-View-Controller design pattern.
|
|
|
|
|
*
|
|
|
|
|
* MVC components in angular:
|
|
|
|
|
*
|
|
|
|
|
* * Model — The Model is data in scope properties; scopes are attached to the DOM.
|
|
|
|
|
* * View — The template (HTML with data bindings) is rendered into the View.
|
2012-04-06 23:35:17 +00:00
|
|
|
* * Controller — The `ngController` directive specifies a Controller class; the class has
|
2012-03-08 23:00:38 +00:00
|
|
|
* methods that typically express the business logic behind the application.
|
|
|
|
|
*
|
2013-06-05 22:30:31 +00:00
|
|
|
* Note that an alternative way to define controllers is via the {@link ngRoute.$route $route} service.
|
2012-03-08 23:00:38 +00:00
|
|
|
*
|
|
|
|
|
* @element ANY
|
|
|
|
|
* @scope
|
2012-04-06 23:35:17 +00:00
|
|
|
* @param {expression} ngController Name of a globally accessible constructor function or an
|
2012-05-24 21:49:47 +00:00
|
|
|
* {@link guide/expression expression} that on the current scope evaluates to a
|
2012-06-06 22:54:40 +00:00
|
|
|
* constructor function. The controller instance can further be published into the scope
|
|
|
|
|
* by adding `as localName` the controller name attribute.
|
2012-03-08 23:00:38 +00:00
|
|
|
*
|
|
|
|
|
* @example
|
|
|
|
|
* Here is a simple form for editing user contact information. Adding, removing, clearing, and
|
|
|
|
|
* greeting are methods declared on the controller (see source tab). These methods can
|
|
|
|
|
* easily be called from the angular markup. Notice that the scope becomes the `this` for the
|
|
|
|
|
* controller's instance. This allows for easy access to the view data from the controller. Also
|
|
|
|
|
* notice that any changes to the data are automatically reflected in the View without the need
|
2012-06-06 22:54:40 +00:00
|
|
|
* for a manual update. The example is included in two different declaration styles based on
|
|
|
|
|
* your style preferences.
|
2012-03-08 23:00:38 +00:00
|
|
|
<doc:example>
|
2012-06-06 22:54:40 +00:00
|
|
|
<doc:source>
|
|
|
|
|
<script>
|
2013-05-23 02:37:42 +00:00
|
|
|
function SettingsController1() {
|
2012-06-06 22:54:40 +00:00
|
|
|
this.name = "John Smith";
|
|
|
|
|
this.contacts = [
|
|
|
|
|
{type: 'phone', value: '408 555 1212'},
|
|
|
|
|
{type: 'email', value: 'john.smith@example.org'} ];
|
|
|
|
|
};
|
|
|
|
|
|
2013-05-23 02:37:42 +00:00
|
|
|
SettingsController1.prototype.greet = function() {
|
2012-06-06 22:54:40 +00:00
|
|
|
alert(this.name);
|
|
|
|
|
};
|
|
|
|
|
|
2013-05-23 02:37:42 +00:00
|
|
|
SettingsController1.prototype.addContact = function() {
|
2012-06-06 22:54:40 +00:00
|
|
|
this.contacts.push({type: 'email', value: 'yourname@example.org'});
|
|
|
|
|
};
|
|
|
|
|
|
2013-05-23 02:37:42 +00:00
|
|
|
SettingsController1.prototype.removeContact = function(contactToRemove) {
|
2012-06-06 22:54:40 +00:00
|
|
|
var index = this.contacts.indexOf(contactToRemove);
|
|
|
|
|
this.contacts.splice(index, 1);
|
|
|
|
|
};
|
|
|
|
|
|
2013-05-23 02:37:42 +00:00
|
|
|
SettingsController1.prototype.clearContact = function(contact) {
|
2012-06-06 22:54:40 +00:00
|
|
|
contact.type = 'phone';
|
|
|
|
|
contact.value = '';
|
|
|
|
|
};
|
|
|
|
|
</script>
|
2013-05-23 02:37:42 +00:00
|
|
|
<div id="ctrl-as-exmpl" ng-controller="SettingsController1 as settings">
|
2012-06-06 22:54:40 +00:00
|
|
|
Name: <input type="text" ng-model="settings.name"/>
|
|
|
|
|
[ <a href="" ng-click="settings.greet()">greet</a> ]<br/>
|
|
|
|
|
Contact:
|
|
|
|
|
<ul>
|
|
|
|
|
<li ng-repeat="contact in settings.contacts">
|
|
|
|
|
<select ng-model="contact.type">
|
|
|
|
|
<option>phone</option>
|
|
|
|
|
<option>email</option>
|
|
|
|
|
</select>
|
|
|
|
|
<input type="text" ng-model="contact.value"/>
|
|
|
|
|
[ <a href="" ng-click="settings.clearContact(contact)">clear</a>
|
|
|
|
|
| <a href="" ng-click="settings.removeContact(contact)">X</a> ]
|
|
|
|
|
</li>
|
|
|
|
|
<li>[ <a href="" ng-click="settings.addContact()">add</a> ]</li>
|
|
|
|
|
</ul>
|
|
|
|
|
</div>
|
|
|
|
|
</doc:source>
|
|
|
|
|
<doc:scenario>
|
2013-05-23 02:37:42 +00:00
|
|
|
it('should check controller as', function() {
|
|
|
|
|
expect(element('#ctrl-as-exmpl>:input').val()).toBe('John Smith');
|
|
|
|
|
expect(element('#ctrl-as-exmpl li:nth-child(1) input').val())
|
2012-06-06 22:54:40 +00:00
|
|
|
.toBe('408 555 1212');
|
2013-05-23 02:37:42 +00:00
|
|
|
expect(element('#ctrl-as-exmpl li:nth-child(2) input').val())
|
2012-06-06 22:54:40 +00:00
|
|
|
.toBe('john.smith@example.org');
|
|
|
|
|
|
2013-05-23 02:37:42 +00:00
|
|
|
element('#ctrl-as-exmpl li:first a:contains("clear")').click();
|
|
|
|
|
expect(element('#ctrl-as-exmpl li:first input').val()).toBe('');
|
2012-06-06 22:54:40 +00:00
|
|
|
|
2013-05-23 02:37:42 +00:00
|
|
|
element('#ctrl-as-exmpl li:last a:contains("add")').click();
|
|
|
|
|
expect(element('#ctrl-as-exmpl li:nth-child(3) input').val())
|
2012-06-06 22:54:40 +00:00
|
|
|
.toBe('yourname@example.org');
|
|
|
|
|
});
|
|
|
|
|
</doc:scenario>
|
|
|
|
|
</doc:example>
|
|
|
|
|
<doc:example>
|
2012-03-08 23:00:38 +00:00
|
|
|
<doc:source>
|
2012-04-29 05:45:28 +00:00
|
|
|
<script>
|
2013-05-23 02:37:42 +00:00
|
|
|
function SettingsController2($scope) {
|
2012-03-08 23:00:38 +00:00
|
|
|
$scope.name = "John Smith";
|
|
|
|
|
$scope.contacts = [
|
|
|
|
|
{type:'phone', value:'408 555 1212'},
|
|
|
|
|
{type:'email', value:'john.smith@example.org'} ];
|
|
|
|
|
|
|
|
|
|
$scope.greet = function() {
|
|
|
|
|
alert(this.name);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
$scope.addContact = function() {
|
|
|
|
|
this.contacts.push({type:'email', value:'yourname@example.org'});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
$scope.removeContact = function(contactToRemove) {
|
|
|
|
|
var index = this.contacts.indexOf(contactToRemove);
|
|
|
|
|
this.contacts.splice(index, 1);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
$scope.clearContact = function(contact) {
|
|
|
|
|
contact.type = 'phone';
|
|
|
|
|
contact.value = '';
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
</script>
|
2013-05-23 02:37:42 +00:00
|
|
|
<div id="ctrl-exmpl" ng-controller="SettingsController2">
|
2012-03-09 08:00:05 +00:00
|
|
|
Name: <input type="text" ng-model="name"/>
|
|
|
|
|
[ <a href="" ng-click="greet()">greet</a> ]<br/>
|
2012-03-08 23:00:38 +00:00
|
|
|
Contact:
|
|
|
|
|
<ul>
|
2012-03-09 08:00:05 +00:00
|
|
|
<li ng-repeat="contact in contacts">
|
|
|
|
|
<select ng-model="contact.type">
|
2012-03-08 23:00:38 +00:00
|
|
|
<option>phone</option>
|
|
|
|
|
<option>email</option>
|
|
|
|
|
</select>
|
2012-03-09 08:00:05 +00:00
|
|
|
<input type="text" ng-model="contact.value"/>
|
|
|
|
|
[ <a href="" ng-click="clearContact(contact)">clear</a>
|
|
|
|
|
| <a href="" ng-click="removeContact(contact)">X</a> ]
|
2012-03-08 23:00:38 +00:00
|
|
|
</li>
|
2012-03-09 08:00:05 +00:00
|
|
|
<li>[ <a href="" ng-click="addContact()">add</a> ]</li>
|
2012-03-08 23:00:38 +00:00
|
|
|
</ul>
|
|
|
|
|
</div>
|
|
|
|
|
</doc:source>
|
|
|
|
|
<doc:scenario>
|
|
|
|
|
it('should check controller', function() {
|
2013-05-23 02:37:42 +00:00
|
|
|
expect(element('#ctrl-exmpl>:input').val()).toBe('John Smith');
|
|
|
|
|
expect(element('#ctrl-exmpl li:nth-child(1) input').val())
|
2012-03-08 23:00:38 +00:00
|
|
|
.toBe('408 555 1212');
|
2013-05-23 02:37:42 +00:00
|
|
|
expect(element('#ctrl-exmpl li:nth-child(2) input').val())
|
2012-03-08 23:00:38 +00:00
|
|
|
.toBe('john.smith@example.org');
|
|
|
|
|
|
2013-05-23 02:37:42 +00:00
|
|
|
element('#ctrl-exmpl li:first a:contains("clear")').click();
|
|
|
|
|
expect(element('#ctrl-exmpl li:first input').val()).toBe('');
|
2012-03-08 23:00:38 +00:00
|
|
|
|
2013-05-23 02:37:42 +00:00
|
|
|
element('#ctrl-exmpl li:last a:contains("add")').click();
|
|
|
|
|
expect(element('#ctrl-exmpl li:nth-child(3) input').val())
|
2012-03-08 23:00:38 +00:00
|
|
|
.toBe('yourname@example.org');
|
|
|
|
|
});
|
|
|
|
|
</doc:scenario>
|
|
|
|
|
</doc:example>
|
2012-06-06 22:54:40 +00:00
|
|
|
|
2012-03-08 23:00:38 +00:00
|
|
|
*/
|
2012-03-22 23:29:31 +00:00
|
|
|
var ngControllerDirective = [function() {
|
2012-03-08 23:00:38 +00:00
|
|
|
return {
|
|
|
|
|
scope: true,
|
|
|
|
|
controller: '@'
|
2012-03-22 23:29:31 +00:00
|
|
|
};
|
2012-03-08 23:00:38 +00:00
|
|
|
}];
|