@@ -36,7 +36,8 @@ In this example we have a simple app which consist of two screens:
36
36
.config(function($routeProvider) {
37
37
$routeProvider.
38
38
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'});
40
41
});
41
42
42
43
AppCntl.$inject = ['$scope', '$route']
@@ -51,23 +52,42 @@ In this example we have a simple app which consist of two screens:
51
52
}
52
53
53
54
function WelcomeCntl($scope) {
54
- $scope.greet = function() {
55
- alert("Hello " + $scope.person.name);
56
- };
57
55
}
58
56
59
57
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
+ }
63
66
64
67
$scope.save = function() {
65
68
angular.copy($scope.form, $scope.person);
66
69
$location.path('/welcome');
67
70
};
68
71
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
+ }
71
91
</file>
72
92
<file name="style.css">
73
93
[ng-view] {
@@ -77,61 +97,71 @@ In this example we have a simple app which consist of two screens:
77
97
}
78
98
79
99
.partial-info {
80
- background-color: blue;
81
- color: white;
82
- padding: 3px;
83
- }
100
+ background-color: blue;
101
+ color: white;
102
+ padding: 3px;
103
+ }
84
104
</file>
85
105
<file name="index.html">
86
106
<div ng-controller="AppCntl">
87
107
<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>
93
111
<div ng-view></div>
94
- <small>Your app footer </small>
112
+ <hr />
113
+ <footer>Your app footer</footer>
95
114
</div>
96
115
</file>
97
116
<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>
100
118
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">
103
121
<option>url</option>
104
122
<option>email</option>
105
123
<option>phone</option>
106
124
</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>
109
127
</div>
110
128
<div>
111
- [ <a href=" " ng: click="form.contacts.$add ()">add</a> ]
129
+ <button title="add " ng- click="addContact ()">+</button>
112
130
</div>
113
131
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>
116
135
</file>
117
136
<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>
125
146
</file>
126
147
<file name="scenario.js">
127
148
it('should navigate to URL', function() {
128
149
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() {
130
154
element('a:contains(Settings)').click();
131
155
input('form.name').enter('yourname');
132
156
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!/);
135
165
});
136
166
</file>
137
167
</example>
@@ -143,9 +173,9 @@ In this example we have a simple app which consist of two screens:
143
173
* Routes are defined in the `AppCntl` class. The initialization of the controller causes the
144
174
initialization of the {@link api/ngRoute.$route $route} service with the proper URL
145
175
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
147
177
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.
150
180
* Changing the URL is sufficient to change the controller and view. It makes no difference whether
151
181
the URL is changed programatically or by the user.
0 commit comments