Skip to content

Commit ebdcf4a

Browse files
committed
docs(deeplinking.ngdoc): correct and enhance deep linking demo
Add "#/" linking Implement ability to add / remove contacts Use semantic tags Closes angular#3870
1 parent 699f86c commit ebdcf4a

File tree

1 file changed

+71
-41
lines changed

1 file changed

+71
-41
lines changed

docs/content/cookbook/deeplinking.ngdoc

+71-41
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ In this example we have a simple app which consist of two screens:
3636
.config(function($routeProvider) {
3737
$routeProvider.
3838
when("/welcome", {templateUrl:'welcome.html', controller:WelcomeCntl}).
39-
when("/settings", {templateUrl:'settings.html', controller:SettingsCntl});
39+
when("/settings", {templateUrl:'settings.html', controller:SettingsCntl}).
40+
otherwise({redirectTo: '/welcome'});
4041
});
4142

4243
AppCntl.$inject = ['$scope', '$route']
@@ -51,23 +52,42 @@ In this example we have a simple app which consist of two screens:
5152
}
5253

5354
function WelcomeCntl($scope) {
54-
$scope.greet = function() {
55-
alert("Hello " + $scope.person.name);
56-
};
5755
}
5856

5957
function SettingsCntl($scope, $location) {
60-
$scope.cancel = function() {
61-
$scope.form = angular.copy($scope.person);
62-
};
58+
59+
$scope.isResetDisabled = function() {
60+
return angular.equals($scope.form, $scope.person)
61+
}
62+
63+
$scope.isSaveDisabled = function () {
64+
return $scope.myForm.$invalid || angular.equals($scope.form, $scope.person)
65+
}
6366

6467
$scope.save = function() {
6568
angular.copy($scope.form, $scope.person);
6669
$location.path('/welcome');
6770
};
6871

69-
$scope.cancel();
70-
}
72+
$scope.init = function() {
73+
$scope.form = angular.copy($scope.person);
74+
}
75+
76+
$scope.cancel = function() {
77+
$scope.init();
78+
$location.path('/welcome');
79+
};
80+
81+
$scope.addContact = function() {
82+
$scope.form.contacts.push({type:'', url:''})
83+
}
84+
85+
$scope.removeContact = function(contact) {
86+
$scope.form.contacts.splice( $scope.form.contacts.indexOf(contact), 1 );
87+
}
88+
89+
$scope.init();
90+
}
7191
</file>
7292
<file name="style.css">
7393
[ng-view] {
@@ -77,61 +97,71 @@ In this example we have a simple app which consist of two screens:
7797
}
7898

7999
.partial-info {
80-
background-color: blue;
81-
color: white;
82-
padding: 3px;
83-
}
100+
background-color: blue;
101+
color: white;
102+
padding: 3px;
103+
}
84104
</file>
85105
<file name="index.html">
86106
<div ng-controller="AppCntl">
87107
<h1>Your App Chrome</h1>
88-
[ <a href="welcome">Welcome</a> | <a href="settings">Settings</a> ]
89-
<hr/>
90-
<span class="partial-info">
91-
Partial: {{$route.current.template}}
92-
</span>
108+
<nav>[ <a href="#/welcome">Welcome</a> | <a href="#/settings">Settings</a> ]</nav>
109+
<hr />
110+
<small class="partial-info" ng-bind="'Partial: '+ ($route.current.loadedTemplateUrl || '(empty)')"></small>
93111
<div ng-view></div>
94-
<small>Your app footer </small>
112+
<hr />
113+
<footer>Your app footer</footer>
95114
</div>
96115
</file>
97116
<file name="settings.html">
98-
<label>Name:</label>
99-
<input type="text" ng:model="form.name" required>
117+
<label>Name: <input type="text" ng-model="form.name" required></label>
100118

101-
<div ng:repeat="contact in form.contacts">
102-
<select ng:model="contact.type">
119+
<div ng-repeat="contact in form.contacts">
120+
<select ng-model="contact.type">
103121
<option>url</option>
104122
<option>email</option>
105123
<option>phone</option>
106124
</select>
107-
<input type="text" ng:model="contact.url">
108-
[ <a href="" ng:click="form.contacts.$remove(contact)">X</a> ]
125+
<input type="text" ng-model="contact.url" />
126+
<button title="remove" ng-click="removeContact(contact)">X</button>
109127
</div>
110128
<div>
111-
[ <a href="" ng:click="form.contacts.$add()">add</a> ]
129+
<button title="add" ng-click="addContact()">+</button>
112130
</div>
113131

114-
<button ng:click="cancel()">Cancel</button>
115-
<button ng:click="save()">Save</button>
132+
<button title="save" ng-click="save()" ng-disabled="isSaveDisabled()">Save</button>
133+
<button title="reset" ng-click="init()" ng-disabled="isResetDisabled()">Reset</button>
134+
<button title="cancel" ng-click="cancel()">Cancel</button>
116135
</file>
117136
<file name="welcome.html">
118-
Hello {{person.name}},
119-
<div>
120-
Your contact information:
121-
<div ng:repeat="contact in person.contacts">{{contact.type}}:
122-
<span ng-bind-html="contact.url|linky"></span>
123-
</div>
124-
</div>
137+
<h2>Hello {{person.name}}!</h2>
138+
<div>
139+
Your contact information:
140+
<ul>
141+
<li ng-repeat="contact in person.contacts">{{contact.type}}:
142+
<span ng-bind-html="contact.url | linky"></span>
143+
</li>
144+
</ul>
145+
</div>
125146
</file>
126147
<file name="scenario.js">
127148
it('should navigate to URL', function() {
128149
element('a:contains(Welcome)').click();
129-
expect(element('[ng-view]').text()).toMatch(/Hello anonymous/);
150+
expect(element('[ng-view]').text()).toMatch(/Hello anonymous!/);
151+
});
152+
153+
it('should save an updated name', function() {
130154
element('a:contains(Settings)').click();
131155
input('form.name').enter('yourname');
132156
element(':button:contains(Save)').click();
133-
element('a:contains(Welcome)').click();
134-
expect(element('[ng-view]').text()).toMatch(/Hello yourname/);
157+
expect(element('[ng-view]').text()).toMatch(/Hello yourname!/);
158+
});
159+
160+
it('should not save a canceled name change', function() {
161+
element('a:contains(Settings)').click();
162+
input('form.name').enter('not to be saved');
163+
element(':button:contains(Cancel)').click();
164+
expect(element('[ng-view]').text()).toMatch(/Hello yourname!/);
135165
});
136166
</file>
137167
</example>
@@ -143,9 +173,9 @@ In this example we have a simple app which consist of two screens:
143173
* Routes are defined in the `AppCntl` class. The initialization of the controller causes the
144174
initialization of the {@link api/ngRoute.$route $route} service with the proper URL
145175
routes.
146-
* The {@link api/ngRoute.$route $route} service then watches the URL and instantiates the
176+
* The {@link api/ngRoute.$route $route} service then watches the URL and instantiates the
147177
appropriate controller when the URL changes.
148-
* The {@link api/ngRoute.directive:ngView ngView} widget loads the
149-
view when the URL changes. It also sets the view scope to the newly instantiated controller.
178+
* The {@link api/ngRoute.directive:ngView ngView} widget loads the view when the URL
179+
changes. It also sets the view scope to the newly instantiated controller.
150180
* Changing the URL is sufficient to change the controller and view. It makes no difference whether
151181
the URL is changed programatically or by the user.

0 commit comments

Comments
 (0)