Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit ae428a0

Browse files
drpicoxNarretz
authored andcommitted
some doc fixes
1 parent 691e65e commit ae428a0

File tree

1 file changed

+160
-67
lines changed

1 file changed

+160
-67
lines changed

src/ng/directive/ngAs.js

+160-67
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,20 @@
66
* @restrict A
77
*
88
* @description
9-
* The `ngAs` attribute tells Angular to assign element component controller
10-
* to a given property.
9+
* The `ngAs` attribute tells Angular to assign the element component controller
10+
* to the given property.
1111
*
1212
* Using this directive you can use the controller of existing components
1313
* in your template (children components).
1414
*
15-
* If the children component is destroyed
16-
* a `null` is assigned to the property.
15+
* If the component is destroyed `null` is assigned to the property.
1716
*
18-
* Note that this is the reverse of `require:`:
19-
* with `require:` is the children who references the parent
20-
* but with `ngAs`is the parent who references the children.
17+
* Note that this is the reverse of `require`:
18+
* * with `require` a component can access to the controllers
19+
* of parent directives or directives in the same element,
20+
* directives outside the component `template:` or `templateUrl`
21+
* * with `ngAs`: a component can access to the controllers
22+
* of components inside its `template` or `templateUrl`
2123
* It is very useful when you want to reuse the same component
2224
* in different situations,
2325
* and they do not need to know which exact parent they have.
@@ -31,21 +33,27 @@
3133
* ### Use inside the scope
3234
* This example shows how the controller of the component toggle
3335
* is reused in the template through the scope to use its logic.
34-
* <example name="ngAsDirectiveComponentExample" module="ngAsExample">
36+
* <example name="ng-as-component" module="myApp">
3537
* <file name="index.html">
36-
* <toggle ng-as="myToggle"></toggle>
38+
* <my-toggle ng-as="myToggle"></my-toggle>
3739
* <button ng-click="myToggle.toggle()">Toggle</button>
38-
* <div ng-show="myToggle.isOpen()">You are using a children component to show it.</div>
40+
* <div ng-show="myToggle.isOpen()">
41+
* You are using a component in the same template to show it.
42+
* </div>
3943
* </file>
40-
* <file name="script.js">
41-
* angular.module('ngAsExample', [])
42-
* .component('toggle', {
43-
* controller: function() {
44-
* var opened = false;
45-
* this.isOpen = function() { return opened; };
46-
* this.toggle = function() { opened = !opened; };
47-
* }
48-
* });
44+
* <file name="index.js">
45+
* angular.module('myApp', []);
46+
* </file>
47+
* <file name="toggle.js">
48+
* function ToggleController() {
49+
* var opened = false;
50+
* this.isOpen = function() { return opened; };
51+
* this.toggle = function() { opened = !opened; };
52+
* }
53+
*
54+
* angular.module('myApp').component('myToggle', {
55+
* controller: ToggleController
56+
* });
4957
* </file>
5058
* <file name="protractor.js" type="protractor">
5159
* it('should publish the toggle into the scope', function() {
@@ -61,58 +69,143 @@
6169
* ### Parent interacts with child via member
6270
* This example shows how the parent controller can have access
6371
* to children component controllers.
64-
* <example name="ngAsDirectiveComponentCounterExample" module="ngAsVoteExample">
65-
* <file name="index.html">
66-
* <competition></competition>
72+
* <example name="ng-as-contest" module="myApp">
73+
* <file name="index.js">
74+
* angular.module('myApp', []);
75+
* </file>
76+
* <file name="contest.js">
77+
* function ContestController() {
78+
* var results = ['y','n','y','y'];
79+
*
80+
* this.$onInit = function() {
81+
* this.restart();
82+
* };
83+
*
84+
* this.hasQuestion = function() {
85+
* return this.currentQuestion <= results.length;
86+
* };
87+
*
88+
* this.nextQuestion = function() {
89+
* var answer = results[this.currentQuestion - 1];
90+
* this.currentQuestion = this.currentQuestion + 1;
91+
*
92+
* this.redScore += score(this.redVoteTaker.vote, answer);
93+
* this.redVoteTaker.clear();
94+
*
95+
* this.blueScore += score(this.blueVoteTaker.vote, answer);
96+
* this.blueVoteTaker.clear();
97+
* };
98+
*
99+
* this.restart = function() {
100+
* this.currentQuestion = 1;
101+
* this.redScore = 0;
102+
* this.blueScore = 0;
103+
* };
104+
*
105+
* function score(vote, expected) {
106+
* if (vote === expected) {
107+
* return +1;
108+
* } else if (vote === null) {
109+
* return 0;
110+
* } else {
111+
* return -1;
112+
* }
113+
* }
114+
* }
115+
*
116+
* angular.module('myApp').component('myContest', {
117+
* controller: ContestController,
118+
* templateUrl: 'contest.html'
119+
* });
67120
* </file>
68-
* <file name="script.js">
69-
* angular.module('ngAsVoteExample', [])
70-
* .component('voteTaker', {
71-
* controller: function() {
72-
* this.vote = null;
73-
* this.agree = function() { this.vote = 'agree'; };
74-
* this.disagree = function() { this.vote = 'disagree'; };
75-
* this.clear = function() { this.vote = null; };
76-
* },
77-
* template:
78-
* '<button ng-disabled="$ctrl.vote" ng-click="$ctrl.agree()">Agree</button>' +
79-
* '<button ng-disabled="$ctrl.vote" ng-click="$ctrl.disagree()">Disagree</button>'
80-
* })
81-
* .component('competition', {
82-
* controller: function() {
83-
* this.redVoteTaker = null;
84-
* this.blueVoteTaker = null;
85-
* this.match = function() {
86-
* return this.redVoteTaker.vote === this.blueVoteTaker.vote && this.redVoteTaker.vote;
87-
* };
88-
* this.next = function() {
89-
* this.sentence++;
90-
* this.redVoteTaker.clear();
91-
* this.blueVoteTaker.clear();
92-
* };
93-
* },
94-
* template:
95-
* '<p>Red team: <vote-taker ng-as="$ctrl.redVoteTaker"></vote-taker></p>' +
96-
* '<p>Blue team: <vote-taker ng-as="$ctrl.blueVoteTaker"></vote-taker></p>' +
97-
* '<p ng-show="$ctrl.match()">There is a match!</p>' +
98-
* '<button ng-click="$ctrl.next()">Next</button>'
99-
* });
121+
* <file name="contest.html">
122+
* <div ng-show="$ctrl.hasQuestion()">
123+
* <p>Question {{$ctrl.currentQuestion}}?</p>
124+
* <p>Red team: <my-vote-taker ng-as="$ctrl.redVoteTaker"></my-vote-taker></p>
125+
* <p>Blue team: <my-vote-taker ng-as="$ctrl.blueVoteTaker"></my-vote-taker></p>
126+
* <p><button ng-click="$ctrl.nextQuestion()">Next Question</button></p>
127+
* </div>
128+
* <div ng-hide="$ctrl.hasQuestion()">
129+
* <p>
130+
* <strong ng-show="$ctrl.redScore > $ctrl.blueScore">Red Wins!</strong>
131+
* <strong ng-show="$ctrl.redScore < $ctrl.blueScore">Blue Wins!</strong>
132+
* <strong ng-show="$ctrl.redScore == $ctrl.blueScore">There is a tie!</strong>
133+
* </p>
134+
* <p>Red score: {{$ctrl.redScore}}</p>
135+
* <p>Blue score: {{$ctrl.blueScore}}</p>
136+
* <p><button ng-click="$ctrl.restart()">Restart</button></p>
137+
* </div>
138+
* </file>
139+
* <file name="voteTaker.js">
140+
* function VoteTakerController() {
141+
* this.vote = null;
142+
*
143+
* this.yes = function() {
144+
* this.vote = 'y';
145+
* };
146+
* this.no = function() {
147+
* this.vote = 'n';
148+
* };
149+
* this.clear = function() {
150+
* this.vote = null;
151+
* };
152+
* }
153+
*
154+
* angular.module('myApp').component('myVoteTaker', {
155+
* controller: VoteTakerController,
156+
* templateUrl: 'voteTaker.html'
157+
* });
158+
* </file>
159+
* <file name="voteTaker.html">
160+
* <button ng-disabled="$ctrl.vote" ng-click="$ctrl.yes()">Yes</button>
161+
* <button ng-disabled="$ctrl.vote" ng-click="$ctrl.no()">No</button>
162+
* </file>
163+
* <file name="index.html">
164+
* <my-contest></my-contest>
100165
* </file>
101166
* <file name="protractor.js" type="protractor">
102-
* var agrees = element.all(by.buttonText('Agree'));
103-
* var matchMessage = element(by.css('[ng-show]'));
104-
* var next = element(by.buttonText('Next'));
105-
*
106-
* it('should show match message if both agree', function() {
107-
* expect(matchMessage.isDisplayed()).toBeFalsy();
108-
* agrees.click();
109-
* expect(matchMessage.isDisplayed()).toBeTruthy();
110-
* });
167+
* function VoteTaker(team) {
168+
* var voteTaker = element(by.css('[ng-as="$ctrl.' + team + 'VoteTaker"]'));
169+
* var yes = voteTaker.element(by.buttonText('Yes'));
170+
* var no = voteTaker.element(by.buttonText('No'));
171+
*
172+
* this.yes = function() {
173+
* yes.click();
174+
* };
175+
* this.no = function() {
176+
* no.click();
177+
* };
178+
* }
179+
*
180+
* function Contest() {
181+
* var redScore = element(by.binding('$ctrl.redScore'));
182+
* var blueScore = element(by.binding('$ctrl.blueScore'));
183+
* var nextQuestion = element(by.buttonText('Next Question'));
184+
*
185+
* this.redVoteTaker = new VoteTaker('red');
186+
* this.blueVoteTaker = new VoteTaker('blue');
187+
*
188+
* this.getRedScore = function() {
189+
* return redScore.getText();
190+
* };
191+
*
192+
* this.getBlueScore = function() {
193+
* return blueScore.getText();
194+
* };
195+
*
196+
* this.nextQuestion = function() {
197+
* nextQuestion.click();
198+
* };
199+
* }
111200
*
112-
* it('should hide match message after next is clicked', function() {
113-
* agrees.click();
114-
* next.click();
115-
* expect(matchMessage.isDisplayed()).toBeFalsy();
201+
* it('should compute score red always yes, blue always pass', function() {
202+
* var contest = new Contest();
203+
* for (var i = 0; i < 4; i++) {
204+
* contest.redVoteTaker.yes();
205+
* contest.nextQuestion();
206+
* }
207+
* expect(contest.getRedScore()).toEqual('Red score: 2');
208+
* expect(contest.getBlueScore()).toEqual('Blue score: 0');
116209
* });
117210
* </file>
118211
* </example>

0 commit comments

Comments
 (0)