Открыть Электронные книги
Категории
Открыть Аудиокниги
Категории
Открыть Журналы
Категории
Открыть Документы
Категории
, Angular -,
.
Angular?
AngularJS -.
HTML HTML,
. ,
, .
JavaScript,
.
Angular , HTML, . HTML
, . ,
, - ,
.
, :
- , -.
, . , Jquery.
- -, .
, - . ,
knockout, SproutCore ..
Angular . HTML
HTML . Angular ,
, . :
, {{}}.
DOM / DOM .
.
DOM .
HTML .
Angular -. ,
-, . Angular
, CRUD . ,
. Angular :
, CRUD : ,
, , , , ,
.
: -, end-to-end , ,
.
.
Angular
Angular , .
, . ,
Angular. Angular CRUD. CRUD
, , 90% -. , Angular ,
, Angular.
, DOM.
CRUD , , , Angular. ,
- JavaScript, JQuery .
Angular
CRUD , .
, ,
. ,
:
.
, .
.
.
I ndex .htm l
1. <!doctype html>
2. <html ng-app>
3. <head>
4. <script src="http://code.angularjs.org/1.0.6/angular.min.js"></script>
5. <script src="script.js"></script>
6. </head>
7. <body>
8. <div ng-controller="InvoiceCntl">
9. <b>Invoice:</b>
10. <br>
11. <br>
12. <table>
13. <tr><td>Quantity</td><td>Cost</td></tr>
14. <tr>
15. <td><input type="number" ng-pattern="/\d+/" step="1" min="0" ng-model="qt
y" required ></td>
16. <td><input type="number" ng-model="cost" required ></td>
17. </tr>
18. </table>
19. <hr>
20. <b>Total:</b> {{qty * cost | currency}}
21. </div>
22. </body>
23. </html>
Script.js
1. function InvoiceCntl($scope) {
2. $scope.qty = 1;
3. $scope.cost = 19.95;
4. }
, , .
{{_expression_}} Angular .
: {{ expression | filter }}.
Angular .
, Angular ,
, .
, , ,
. ,
, , , .
Angular .
Angular
Angular , , ,
,
-.
, DOM .
.
, ,
. .
, .
, .
, :
, -, .
, , , .
Angular :
: ,
. ,
. JavaScript ,
, , .
HTML DOM : DOM HTML
AJAX, . ,
,
DOM . ,
Angular, DOM, , .
: CRUD
AJAX. HTML-,
, , ,
, ,
. Angular , ,
, .
, Angular, .
Angular <script>
Angular .
<!doctype html>
<html xmlns:ng="http://angularjs.org" ng-app>
<body>
...
<script src="angular.js">
</body>
</html>
<script> .
, .. HTML
. agular
http://code.angularjs.org.
. :
,
- angular-[version].js.
- angular-[version].min.js
ng-app ,
<html>, angular .
<html ng-app>
, .
XML. :
<html xmlns:ng="http://angularjs.org">
DOMContentLoaded Angular .
ng-app, .
, :
, .
.
DOM.
DOM angular.
<!doctype html>
<html ng-app="optionalModuleName">
<body>
I can add: {{ 1+2 }}.
<script src="angular.js"></script>
</body>
</html>
,
. , ,
- .
. ng-app.
1. <!doctype html>
2. <html xmlns:ng="http://angularjs.org">
3. <body>
4. Hello {{'World'}}!
5. <script src="http://code.angularjs.org/angular.js"></script>
6. <script>
7. angular.element(document).ready(function() {
8. angular.bootstrap(document);
9. });
10. </script>
11. </body>
12. </html>
, angular (
html).
API angular.bootstrap
.
HTML
HTML
. ,
,
. align=center, .
.
, ..
. 1/3
.
.
Angular ,
. ,
,
.
, .
angular . DOM
. :
1. DOM .
.
2. , (scope)
(view).
,
.
.
, ng-repeat DOM
.
, ,
.
.
.
: , ,
class, . ng-
bind.
1. <span ng-bind="exp"></span>
2. <span class="ng-bind: exp;"></span>
3. <ng-bind></ng-bind>
4. <!-- directive: ng-bind exp -->
, ,
DOM. , .
index.html:
1. <!doctype html>
2. <html ng-app="drag">
3. <head>
4. <script src="http://code.angularjs.org/1.0.6/angular.min.js"></script>
5. <script src="script.js"></script>
6. </head>
7. <body>
8. <span draggable>Drag ME</span>
9. </body>
10. </html>
script.js
1. angular.module('drag', []).
2. directive('draggable', function($document) {
3. return function(scope, element, attr) {
4. var startX = 0, startY = 0, x = 0, y = 0;
5. element.css({
6. position: 'relative',
7. border: '1px solid red',
8. backgroundColor: 'lightgrey',
9. cursor: 'pointer'
10. });
11. element.bind('mousedown', function(event) {
12. // Prevent default dragging of selected content
13. event.preventDefault();
14. startX = event.screenX - x;
15. startY = event.screenY - y;
16. $document.bind('mousemove', mousemove);
17. $document.bind('mouseup', mouseup);
18. });
19.
20. function mousemove(event) {
21. y = event.screenY - startY;
22. x = event.screenX - startX;
23. element.css({
24. top: y + 'px',
25. left: x + 'px'
26. });
27. }
28.
29. function mouseup() {
30. $document.unbind('mousemove', mousemove);
31. $document.unbind('mouseup', mouseup);
32. }
33. }
34. });
draggable. ,
. ,
, , HTML.
view.
.
, .
HTML innerHTML.
innerHTML DOM. ,
:
.
.
.
.
Angular . DOM, ,
. ,
(scope) .
.
. .. innerHTML ,
. , angular
, .
DOM,
DOM
. ,
, .
angular,
. :
(startup) .
(runtime) angular .
(scope) .
.
.
, .
HTML.
.
.
.
$ - angular .
, .
,
.
1. HTML
DOM.
2. angular.js angular.
3. Angular DOMContentLoaded.
4. Angular ng-app, .
5. , ng-app ( ) .
6. $compile, $rootScope.
7. $compile DOM
$rootScope.
8. ng-init $scope .
9. {{name}} name
$scope $rootScope.
Index.html
1. <!doctype html>
2. <html ng-app>
3. <head>
4. <script src="http://code.angularjs.org/1.0.6/angular.min.js"></script>
5. </head>
6. <body>
7. <p ng-init=" name='World' ">Hello {{name}}!</p>
8. </body>
9. </html>
, angular .
1.
,
.
2.
,
DOM.
3.
javascript
DOM .
Angular
javascript, . javascript
angular . ,
angular, , ,
.. $apply()
angular javascript. , (, )
$apply() , , .
$apply()
.
hello world,
.
1. :
1.1. Ng-model input keydown
input.
1.2. {{name}} name, $watch
name.
2. :
2.1. x input
keydown.
2.2. input input $apply(name=x)
Angular.
2.3. Angular name x.
2.4. $digest.
2.5. $watch name ,
DOM.
2.6. Angular,
keydown, javascript.
2.7. .
Index.html
1. <!doctype html>
2. <html ng-app>
3. <head>
4. <script src="http://code.angularjs.org/1.0.6/angular.min.js"></script>
5. </head>
6. <body>
7. <input ng-model="name">
8. <p>Hello {{name}}!</p>
9. </body>
10. </html>
(scope)
Scope
. Scope
DOM. ( ,
scope).
, name ,
, .
Index.html
1. <!doctype html>
2. <html ng-app>
3. <head>
4. <script src="http://code.angularjs.org/1.0.6/angular.min.js"></script>
5. <script src="script.js"></script>
6. </head>
7. <body>
8. <div ng-controller="GreetCtrl">
9. Hello {{name}}!
10. </div>
11. <div ng-controller="ListCtrl">
12. <ol>
13. <li ng-repeat="name in names">{{name}}</li>
14. </ol>
15. </div>
16. </body>
17. </html>
Style.css
1. .show-scope .doc-example-live.ng-scope,
2. .show-scope .doc-example-live .ng-scope {
3. border: 1px solid red;
4. margin: 3px;
5. }
Script.js
1. function GreetCtrl($scope) {
2. $scope.name = 'World';
3. }
4.
5. function ListCtrl($scope) {
6. $scope.names = ['Igor', 'Misko', 'Vojta'];
7. }
.
.
scope . Scope
,
.
:
javascript,
.
.
(DOM HTML ).
HTML, .
.
.
,
.
(re-skinning), (
), .
Index.html
1. <!doctype html>
2. <html ng-app>
3. <head>
4. <script src="http://code.angularjs.org/1.0.6/angular.min.js"></script>
5. <script src="script.js"></script>
6. </head>
7. <body>
8. <div ng-controller="MyCtrl">
9. Hello {{name}}!
10. <button ng-click="action()">
11. OK
12. </button>
13. </div>
14. </body>
15. </html>
script.js
1. function MyCtrl($scope) {
2. $scope.action = function() {
3. $scope.name = 'OK';
4. }
5.
6. $scope.name = 'World';
7. }
,
.
,
scope.
Angular
.
,
. ,
. javascript.
, , .
, , .
Angular
,
:
,
.
HTML,
innerHTML.
.
,
DOM .
Angular
,
DOM, . ,
HTML, ,
DOM, Angular
($compile). DOM ,
$watch. ,
.
Index.html
1. <!doctype html>
2. <html ng-app>
3. <head>
4. <script src="http://code.angularjs.org/1.0.6/angular.min.js"></script>
5. </head>
6. <body>
7. <div ng-init="list = ['Chrome', 'Safari', 'Firefox', 'IE'] ">
8. <input ng-model="list" ng-list> <br>
9. <input ng-model="list" ng-list> <br>
10. <pre>list={{list}}</pre> <br>
11. <ol>
12. <li ng-repeat="item in list">
13. {{item}}
14. </li>
15. </ol>
16. </div>
17. </body>
18. </html>
DOM, HTML
, .
HTML,
.
contentEditable.
Index.html
1. <!doctype html>
2. <html ng-app="directive">
3. <head>
4. <script src="http://code.angularjs.org/1.0.6/angular.min.js"></script>
5. <script src="script.js"></script>
6. </head>
7. <body>
8. <div contentEditable="true" ng-model="content">Edit Me</div>
9. <pre>model = {{content}}</pre>
10. </body>
11. </html>
Style.css
1. div[contentEditable] {
2. cursor: pointer;
3. background-color: #D0D0D0;
4. margin-bottom: 1em;
5. padding: 1em;
6. }
Script.js
.
(
). UNIX .
Index.html
1. <!doctype html>
2. <html ng-app>
3. <head>
4. <script src="http://code.angularjs.org/1.0.6/angular.min.js"></script>
5. </head>
6. <body>
7. <div ng-init="list = ['Chrome', 'Safari', 'Firefox', 'IE'] ">
8. Number formatting: {{ 1234567890 | number }} <br>
9. array filtering <input ng-model="predicate">
10. {{ list | filter:predicate | json }}
11. </div>
12. </body>
13. </html>
,
Angular.
.
,
. ,
, .
, .
1. //
2. var myModule = angular.module('myModule', [])
3.
4. //
5. myModule.factory('serviceA', function() {
6. return {
7. // {},
8. };
9. });
10.
11. // 'myModule'
12. var $injector = angular.injector(['myModule']);
13.
14. //
15. var serviceA = $injector.get('serviceA');
16.
17. // , .. .
18. $injector.get('serviceA') === $injector.get('serviceA');
, .
, .
1. // , .
2. function doSomething(serviceA, serviceB) {
3. // do something here.
4. }
5.
6. // Angular
7. var $injector = ...;
8.
9. ///////////////////////////////////////////////
10. // .
11. var serviceA = $injector.get('serviceA');
12. var serviceB = $injector.get('serviceB');
13.
14. //
15. doSomething(serviceA, serviceB);
16.
17. ///////////////////////////////////////////////
18. // .
19. // $injector
20. $injector.invoke(doSomething); //
, ,
. Angular
.
ClockCtrl , ,
. ng-controller ,
, , .
, .
Index.html
1. <!doctype html>
2. <html ng-app="timeExampleModule">
3. <head>
4. <script src="http://code.angularjs.org/1.0.6/angular.min.js"></script>
5. <script src="script.js"></script>
6. </head>
7. <body>
8. <div ng-controller="ClockCtrl">
9. Current time is: {{ time.now }}
10. </div>
11. </body>
12. </html>
Script.js
1. angular.module('timeExampleModule', []).
2. // , ,
3. //
4. factory('time', function($timeout) {
5. var time = {};
6.
7. (function tick() {
8. time.now = new Date().toString();
9. $timeout(tick, 1000);
10. })();
11. return time;
12. });
13.
14. // ,
15. // .
16. function ClockCtrl($scope, time) {
17. $scope.time = time;
18. }
Angular
Angular $. ,
, .
HTML.
DOM, HTML .
DOM.
Angular ,
, , HTML
(DSL).
HTML.
, ngBind.
, ,
, :, _. x-
data-, , ,
HTML. : ng:bind,
ng_bind, ng-bind, x-ng-bind data-ng-bind.
, , .
myDir (,
).
1. <span my-dir="exp"></span>
2. <span class="my-dir: exp;"></span>
3. <my-dir></my-dir>
4. <!-- directive: my-dir exp -->
,
, .
Index.html
1. <!doctype html>
2. <html ng-app>
3. <head>
4. <script src="http://code.angularjs.org/1.0.6/angular.min.js"></script>
5. <script src="script.js"></script>
6. </head>
7. <body>
8. <div ng-controller="Ctrl1">
9. Hello <input ng-model='name'> <hr/>
10. <span ng:bind="name"> <span ng:bind="name"></span> <br/>
11. <span ng_bind="name"> <span ng_bind="name"></span> <br/>
12. <span ng-bind="name"> <span ng-bind="name"></span> <br/>
13. <span data-ng-bind="name"> <span data-ng-bind="name"></span> <br/>
14. <span x-ng-bind="name"> <span x-ng-bind="name"></span> <br/>
15. </div>
16. </body>
17. </html>
Script.js
1. function Ctrl1($scope) {
2. $scope.name = 'angular';
3. }
$interpolate
, .
$watch $digest.
.
.
HTML :
1. DOM HTML.
, .
, , DOM
.
2. DOM $compile().
DOM, . ,
, DOM.
DOM,
. DOM,
link , . $compile
, ,
.
3. scope, ,
.
, ,
scope.
DOM scope. scope DOM.
, ,
DOM .
,
. {{user}} - , ngRepeat .
ngRepeat . li
user.actions. li
, ul.
li . ,
, {{action.description}}. ,
li, .
, DOM,
. ,
100 ,
.
,
,
li scope.
ngRepeat li . ngRepeat li
. li ,
li, scope.
ngRepeat ,
, scope, li
.
,
DOM,
DOM. ,
,
.
.
DOM
, scope DOM.
( )
, .
Index.html
1. <!doctype html>
2. <html ng-app="time">
3. <head>
4. <script src="http://code.angularjs.org/1.0.6/angular.min.js"></script>
5. <script src="script.js"></script>
6. </head>
7. <body>
8. <div ng-controller="Ctrl2">
9. Date format: <input ng-model="format"> <hr/>
10. Current time is: <span my-current-time="format"></span>
11. </div>
12. </body>
13. </html>
Script.js
1. function Ctrl2($scope) {
2. $scope.format = 'M/d/yy h:mm:ss a';
3. }
4.
5. angular.module('time', [])
6. // 'myCurrentTime'.
7. // $timeout dateFilter .
8. .directive('myCurrentTime', function($timeout, dateFilter) {
9. // . ( )
10. return function(scope, element, attrs) {
11. var format, //
12. timeoutId; //
13.
14. //
15. function updateTime() {
16. element.text(dateFilter(new Date(), format));
17. }
18.
19. // UI.
20. scope.$watch(attrs.myCurrentTime, function(value) {
21. format = value;
22. updateTime();
23. });
24.
25. //
26. function updateLater() {
27. //
28. timeoutId = $timeout(function() {
29. updateTime(); // DOM
30. updateLater(); //
31. }, 1000);
32. }
33.
34. // destroy UI
35. // .
36. element.bind('$destroy', function() {
37. $timeout.cancel(timeoutId);
38. });
39.
40. updateLater(); // UI.
41. }
42. });
( )
.
. ,
(. ), postLink ,
.
, .
, ,
. postLink.
. ,
.
. $injector.invoke,
.
. :
name (scope).
.
priority , DOM,
.
. ,
.
.
terminal true,
, (
).
scope :
true .
, ,
.
, .
{}( ) .
,
.
,
.
,
.
.
.
@ @attr DOM.
, .. DOM .
, .
<widget my-attr="hello {{name}}">
scope: { localName:'@myAttr' }
widget hello
{{name}}. name,
localName. name (
).
= =attr attr
. attr ,
. <widget my-attr="parentModel">
scope: {
localModel:'=myAttr' },
localModel
parenModel .
parentModel
localModel , .
& &attr .
attr ,
. <widget my-attr="count = count + value">,
scope: { localFn:'&myAttr' },
localFn
count = count + value.
,
,
localFn .
, increment(amount),
localFn,
localFn(amount:22).
controller .
, . (. require).
.
:
$scope , .
$element .
$attrs .
$transclude ,
: function(cloneLinkingFn).
require
. Require .
, . :
? . .
^ - .
restrict ,
EACM,
.
E : <my-directive></my-directive>
A : <div my-directive="exp"> </div>
C : <div class="my-directive: exp;"></div>
M : <!-- directive: my-directive exp -->
template HTML ,
.
.
.
templateUrl , , url.
,
.
replace true,
, .
transclude .
ngTransclude. ,
.
, transclude
, - .
.
true
element , ,
.
compile , .
link , .
, compile .
(compile)
function compile(tElement, tAttrs, transclude) { ... }
DOM.
DOM, . ,
, DOM,
ngRepeat, , ngView.
:
tElement .
.
tAttrs ,
, . . .
transclude : function(scope, cloneLinkingFn).
:
, . ,
, DOM,
. DOM,
, .
, .
link,
,
compile .
pre post,
,
(pre) (post). - -
.
(link)
function link(scope, iElement, iAttrs, controller) { ... }
DOM,
, . .
.
scope ,
$watch.
iElement - , .
postLink
, .
iAttrs ,
, . . .
controller ,
.
, .
- (pre-linking)
, .
DOM, ..
.
- (post-linking)
.
.
link compile,
:
1.
,
.
2. ,
.
3. ,
. .
4.
, $observe.
,
.
.
. ,
, .
1. <div>
2. <button ng-click="show=true">show</button>
3. <dialog title="Hello {{username}}."
4. visible="show"
5. on-cancel="show = false"
6. on-ok="show = false; doSomething()">
7. Body goes here: {{username}} is {{title}}.
8. </dialog>
9. </div>
show . ,
username, , . ,
.
1. <div ng-show="visible">
2. <h3>{{title}}</h3>
3. <div class="body" ng-transclude></div>
4. <div class="footer">
5. <button ng-click="onOk()">Save changes</button>
6. <button ng-click="onCancel()">Close</button>
7. </div>
8. </div>
, , title,
username. ,
onOk onCancel, .
. , ,
, .
1. scope: {
2. title: '@', //
3. onOk: '&', //
4. onCancel: '&', //
5. visible: '=' //
6. }
1. title ,
.
2. DOM ,
, . title
title .
.
, , .
, DOM
, ,
.
, ( ) .
, - (
).
, .
1. transclude: true,
2. scope: {
3. title: '@', //
4. onOk: '&', //
5. onCancel: '&', //
6. visible: '=' // -
7. },
8. restrict: 'E',
9. replace: true
DOM.
, .
Index.html
1. <!doctype html>
2. <html ng-app="zippyModule">
3. <head>
4. <script src="http://code.angularjs.org/1.0.6/angular.min.js"></script>
5. <script src="script.js"></script>
6. </head>
7. <body>
8. <div ng-controller="Ctrl3">
9. Title: <input ng-model="title"> <br>
10. Text: <textarea ng-model="text"></textarea>
11. <hr>
12. <div class="zippy" zippy-title="Details: {{title}}...">{{text}}</div>
13. </div>
14. </body>
15. </html>
Style.css
1. .zippy {
2. border: 1px solid black;
3. display: inline-block;
4. width: 250px;
5. }
6. .zippy.opened > .title:before { content: ' '; }
7. .zippy.opened > .body { display: block; }
8. .zippy.closed > .title:before { content: ' '; }
9. .zippy.closed > .body { display: none; }
10. .zippy > .title {
11. background-color: black;
12. color: white;
13. padding: .1em .3em;
14. cursor: pointer;
15. }
16. .zippy > .body {
17. padding: .1em .3em;
18. }
Script.js
1. function Ctrl3($scope) {
2. $scope.title = 'Lorem Ipsum';
3. $scope.text = 'Neque porro quisquam est qui dolorem ipsum quia dolor...';
4. }
5.
6. angular.module('zippyModule', [])
7. .directive('zippy', function(){
8. return {
9. restrict: 'C',
10. // zippy.
11. replace: true,
12. transclude: true,
13. scope: { title:'@zippyTitle' },
14. template: '<div>' +
15. '<div class="title">{{title}}</div>' +
16. '<div class="body" ng-transclude></div>' +
17. '</div>',
18. //
19. link: function(scope, element, attrs) {
20. // Title
21. var title = angular.element(element.children()[0]),
22. // Opened / closed
23. opened = true;
24.
25. // /
26. title.bind('click', toggle);
27.
28. //
29. function toggle() {
30. opened = !opened;
31. element.removeClass(opened ? 'closed' : 'opened');
32. element.addClass(opened ? 'opened' : 'closed');
33. }
34.
35. // zippy
36. toggle();
37. }
38. }
39. });
JavaScript ,
. ( {{expression}}).
$parse.
Angular:
1+2
3*10 | currency
user.name
Angular JavaScript
, Angular JavaScript. Angular
eval(), . Angular JavaScript :
angular ,
JavaScript .
angular null,
JavaScript NullPointerExceptions.
angular
, .
angular
. .
JavaScript,
. eval()
JavaScript angular, $eval().
Index.html
1. <!doctype html>
2. <html ng-app>
3. <head>
4. <script src="http://code.angularjs.org/1.0.6/angular.min.js"></script>
5. </head>
6. <body>
7. 1+2={{1+2}}
8. </body>
9. </html>
, :
Index.html
1. <!doctype html>
2. <html ng-app>
3. <head>
4. <script src="http://code.angularjs.org/1.0.6/angular.min.js"></script>
5. <script src="script.js"></script>
6. </head>
7. <body>
8. <div ng-controller="Cntl2" class="expressions">
9. Expression:
10. <input type='text' ng-model="expr" size="80"/>
11. <button ng-click="addExp(expr)">Evaluate</button>
12. <ul>
13. <li ng-repeat="expr in exprs">
14. [ <a href="" ng-click="removeExp($index)">X</a> ]
15. <tt>{{expr}}</tt> => <span ng-bind="$parent.$eval(expr)"></span>
16. </li>
17. </ul>
18. </div>
19. </body>
20. </html>
Script.js
1. function Cntl2($scope) {
2. var exprs = $scope.exprs = [];
3. $scope.expr = '3*10|currency';
4. $scope.addExp = function(expr) {
5. exprs.push(expr);
6. };
7.
8. $scope.removeExp = function(index) {
9. exprs.splice(index, 1);
10. };
11. }
. JavaScript,
, $window. ,
alert() ,
$window.alert(). ,
( ).
Index.html
1. <!doctype html>
2. <html ng-app>
3. <head>
4. <script src="http://code.angularjs.org/1.0.6/angular.min.js"></script>
5. <script src="script.js"></script>
6. </head>
7. <body>
8. <div class="example2" ng-controller="Cntl1">
9. Name: <input ng-model="name" type="text"/>
10. <button ng-click="greet()">Greet</button>
11. </div>
12. </body>
13. </html>
Script.js
null. JavaScript a.b.c , a
b. , angular
, :
{{a.b.c}}
,
, .
,
, , : {{((a||{}).b||{}).c}}.
null,
undefined.
.
angular, ,
. ,
, .
, . , ,
,
. , :
name | uppercase
name uppercase,
. :
,
:
123 | number:2
$
, . angular api
. angular , a.length(),
undefined, a, angular .
, angular ,
. ,
, .
, .
ngModel.
,
. , api, ,
.
Index.html
1. <!doctype html>
2. <html ng-app>
3. <head>
4. <script src="http://code.angularjs.org/1.0.6/angular.min.js"></script>
5. <script src="script.js"></script>
6. </head>
7. <body>
8. <div ng-controller="Controller">
9. <form novalidate class="simple-form">
10. Name: <input type="text" ng-model="user.name" /><br />
11. E-mail: <input type="email" ng-model="user.email" /><br />
12. Gender: <input type="radio" ng-model="user.gender" value="male" />male
13. <input type="radio" ng-model="user.gender" value="female" />female<br />
14. <button ng-click="reset()">RESET</button>
15. <button ng-click="update(user)">SAVE</button>
16. </form>
17. <pre>form = {{user | json}}</pre>
18. <pre>master = {{master | json}}</pre>
19. </div>
20. </body>
21. </html>
Script.js
1. function Controller($scope) {
2. $scope.master= {};
3.
4. $scope.update = function(user) {
5. $scope.master= angular.copy(user);
6. };
7.
8. $scope.reset = function() {
9. $scope.user = angular.copy($scope.master);
10. };
11.
12. $scope.reset();
13. }
: novalidate
.
CSS
, ngModel
:
ng-valid
ng-invalid
ng-pristine
ng-dirty
CSS,
. user.name user.email ,
, . ,
, ,
.
Index.html
1. <!doctype html>
2. <html ng-app>
3. <head>
4. <script src="http://code.angularjs.org/1.0.6/angular.min.js"></script>
5. <script src="script.js"></script>
6. </head>
7. <body>
8. <div ng-controller="Controller">
9. <form novalidate class="css-form">
10. Name:
11. <input type="text" ng-model="user.name" required /><br />
12. E-mail: <input type="email" ng-model="user.email" required /><br />
13. Gender: <input type="radio" ng-model="user.gender" value="male" />male
14. <input type="radio" ng-model="user.gender" value="female" />female<br />
15. <button ng-click="reset()">RESET</button>
16. <button ng-click="update(user)">SAVE</button>
17. </form>
18. </div>
19.
20. <style type="text/css">
21. .css-form input.ng-invalid.ng-dirty {
22. background-color: #FA787E;
23. }
24.
25. .css-form input.ng-valid.ng-dirty {
26. background-color: #78FA89;
27. }
28. </style>
29. </body>
30. </html>
Script.js
1. function Controller($scope) {
2. $scope.master = {};
3.
4. $scope.update = function(user) {
5. $scope.master = angular.copy(user);
6. };
7.
8. $scope.reset = function() {
9. $scope.user = angular.copy($scope.master);
10. };
11.
12. $scope.reset();
13. }
FormController. , ,
, name. ,
NgModelController. ,
name. ,
, .
, .
, .
, user.email user.agree.
Index.html
1. <!doctype html>
2. <html ng-app>
3. <head>
4. <script src="http://code.angularjs.org/1.0.6/angular.min.js"></script>
5. <script src="script.js"></script>
6. </head>
7. <body>
8. <div ng-controller="Controller">
9. <form name="form" class="css-form" novalidate>
10. Name:
11. <input type="text" ng-model="user.name" name="uName" required /><br />
12. E-mail:
13. <input type="email" ng-model="user.email" name="uEmail" required/><br />
14. <div ng-show="form.uEmail.$dirty && form.uEmail.$invalid">Invalid:
15. <span ng-show="form.uEmail.$error.required">Tell us your email.</span>
16. <span ng-show="form.uEmail.$error.email">This is not a valid email.</spa
n>
17. </div>
18.
19. Gender: <input type="radio" ng-model="user.gender" value="male" />male
20. <input type="radio" ng-model="user.gender" value="female" />female<br />
21.
22. <input type="checkbox" ng-model="user.agree" name="userAgree" required />
23. I agree: <input ng-show="user.agree" type="text" ng-model="user.agreeSign"
24. required /><br />
25. <div ng-show="!user.agree || !user.agreeSign">Please agree and sign.</div>
26.
27. <button ng-click="reset()" ng-disabled="isUnchanged(user)">RESET</button>
28. <button ng-click="update(user)"
29. ng-disabled="form.$invalid || isUnchanged(user)">SAVE</button>
30. </form>
31. </div>
32. </body>
33. </html>
Script.js
1. function Controller($scope) {
2. $scope.master= {};
3.
4. $scope.update = function(user) {
5. $scope.master= angular.copy(user);
6. };
7.
8. $scope.reset = function() {
9. $scope.user = angular.copy($scope.master);
10. };
11.
12. $scope.isUnchanged = function(user) {
13. return angular.equals(user, $scope.master);
14. };
15.
16. $scope.reset();
17. }
Angular
HTML5 (text, number, url, email, radio, checkbox),
(required, pattern, minlength, maxlength, min, max).
, ,
ngModel. ,
, . :
1. ,
, NgModelController#$formatters,
NgModelController#$setValidity.
2. ,
, NgModelController#$setViewValue.
NgModelController#$parsers,
NgModelController#$setValidity.
1. integer , .
1,23 , .. .
, unshift parsers,
. , ,
, .
2. smart-float. 1,2 1.2
1.2.
number, ..
.
Index.html
1. <!doctype html>
2. <html ng-app="form-example1">
3. <head>
4. <script src="http://code.angularjs.org/1.0.6/angular.min.js"></script>
5. <script src="script.js"></script>
6. </head>
7. <body>
8. <div ng-controller="Controller">
9. <form name="form" class="css-form" novalidate>
10. <div>
11. Size (integer 0 - 10):
12. <input type="number" ng-model="size" name="size"
13. min="0" max="10" integer />{{size}}<br />
14. <span ng-show="form.size.$error.integer">This is not valid integer!</spa
n>
15. <span ng-show="form.size.$error.min || form.size.$error.max">
16. The value must be in range 0 to 10!</span>
17. </div>
18.
19. <div>
20. Length (float):
21. <input type="text" ng-model="length" name="length" smart-float />
22. {{length}}<br />
23. <span ng-show="form.length.$error.float">
24. This is not a valid float number!</span>
25. </div>
26. </form>
27. </div>
28. </body>
29. </html>
Script.js
(
ngModel)
Angular (input, select, textarea),
. , ,
.
, ngModel
, :
$render, ,
NgModelController#$formatters.
NgModelController#$setViewValue ,
.
$compileProvider.directive .
,
.
Index.html
1. <!doctype html>
2. <html ng-app="form-example2">
3. <head>
4. <script src="http://code.angularjs.org/1.0.6/angular.min.js"></script>
5. <script src="script.js"></script>
6. </head>
7. <body>
8. <div contentEditable="true" ng-model="content" title="Click to edit">Some</div
>
9. <pre>model = {{content}}</pre>
10.
11. <style type="text/css">
12. div[contentEditable] {
13. cursor: pointer;
14. background-color: #D0D0D0;
15. }
16. </style>
17. </body>
18. </html>
Script.js
1. angular.module('form-example2', []).directive('contenteditable', function() {
2. return {
3. require: 'ngModel',
4. link: function(scope, elm, attrs, ctrl) {
5. // view -> model
6. elm.bind('blur', function() {
7. scope.$apply(function() {
8. ctrl.$setViewValue(elm.html());
9. });
10. });
11.
12. // model -> view
13. ctrl.$render = function() {
14. elm.html(ctrl.$viewValue);
15. };
16.
17. // load init value from DOM
18. ctrl.$setViewValue(elm.html());
19. }
20. };
21. });
i18n l10n
i18n l10n Angular.
, i18n, ,
. , l10n,
.
( , ) .
, .
angular
ngPluralize.
angular
$locale.
, , ,
, Angular
. Github, i18n/l10n
Angular.
?
, .
, :
. en-US, en-AU, zh-CH, ,
. ,
, en, zh. ICU
.
angular?
Angular
. .
Angular.
Angular :
Angular.
,
angular.
*nix ,
, :
angular_de-ge.js,
angular.js, Angular .
js index.html
index.html.
index.html :
1. <html ng-app>
2. <head>
3. .
4. <script src="angular.js"></script>
5. <script src="i18n/angular-locale_de-ge.js"></script>
6. .
7. </head>
8. </html>
, (
index.html, angular-locale.js) ,
. ,
.
, , ..
.
gotcha
Angular ,
$locale,
, .
, . ,
,
, .
, 1000$,
, {{1000 | currency}},
, 1000 .
.
.
, .
gotcha
, ,
. ,
,
.
, angular datetime .
angular, JavaScript
.
IE
IE.
angular IE8.0 .
, Angular IE, , :
1. IE JSON.stringify (IE7.0 ), ,
JSON2 JSON3 .
1. <!doctype html>
2. <html xmlns:ng="http://angularjs.org">
3. <head>
4. <!--[if lte IE 8]>
5. <script src="/path/to/json2.js"></script>
6. <![endif]-->
7. </head>
8. <body>
9. ...
10. </body>
11. </html>
2. id=ng-app , ng-app.
1. <!doctype html>
2. <html xmlns:ng="http://angularjs.org" id="ng-app" ng-app="optionalModuleName">
3. ...
4. </html>
3. , <ng:view>,
- <div ng-view>,
4. ,
, IE:
1. <!doctype html>
2. <html xmlns:ng="http://angularjs.org" id="ng-app" ng-app="optionalModuleName">
3. <head>
4. <!--[if lte IE 8]>
5. <script>
6. document.createElement('ng-include');
7. document.createElement('ng-pluralize');
8. document.createElement('ng-view');
9.
10. // Optionally these for CSS
11. document.createElement('ng:include');
12. document.createElement('ng:pluralize');
13. document.createElement('ng:view');
14. </script>
15. <![endif]-->
16. </head>
17. <body>
18. ...
19. </body>
20. </html>
xmlns:ng - namespace,
, .
document.createElement(yourTagName), IE.
IE ,
HTML. , :
my:,
XML, <html xmlns:my="ignored">
:, HTML,
document.createElement('my-tag')
CSS,
document.createElement('my-tag')
.
, ,
.
, ?
, myTag ( , ).
1. <html>
2. <body>
3. <mytag>some text</mytag>
4. </body>
5. </html>
DOM.
1. #document
2. +- HTML
3. +- BODY
4. +- mytag
5. +- #text: some text
, BODY mytag,
. IE , :
1. #document
2. +- HTML
3. +- BODY
4. +- mytag
5. +- #text: some text
6. +- /mytag
, BODY :
mytag. , <br/>.
, / .
, , mytag,
.
/ mytag. ,
/. ,
DOM, .
CSS
CSS
document.createElement('my-tag') , ,
.
Angular , JavaScript.
(HTML, CSS, JavaScript),
- .
, angular
, . Angular
, :
DOM
,
, .
, angular ,
, ..:
,
( -,
)
( )
HTML ( JavaScript)
.
Angular -,
. ,
, .
Angular ,
- .
, angular .
.
?
,
. Angular . ,
. :
- ,
-.
,
end-to-end
.
/ (-
).
, . Hello world ?
Module API
myApp ng-app="myApp",
.
:
I ndex .htm l
1. <!doctype html>
2. <html ng-app="myApp">
3. <head>
4. <script src="http://code.angularjs.org/1.0.6/angular.min.js"></script>
5. <script src="script.js"></script>
6. </head>
7. <body>
8. <div>
9. {{ 'World' | greet }}
10. </div>
11. </body>
12. </html>
Script.js
1. //
2. var myAppModule = angular.module('myApp', []);
3.
4. // .
5. //
6. myAppModule.filter('greet', function() {
7. return function(name) {
8. return 'Hello, ' + name + '!';
9. };
10. });
, .
, , :
Service , .
Directives ,
Filters ,
, ,
.
, ,
, . ,
. ,
.
, , .
:
I ndex .htm l
1. <!doctype html>
2. <html ng-app="xmpl">
3. <head>
4. <script src="http://code.angularjs.org/1.0.6/angular.min.js"></script>
5. <script src="script.js"></script>
6. </head>
7. <body>
8. <div ng-controller="XmplController">
9. {{ greeting }}!
10. </div>
11. </body>
12. </html>
Script.js
1. angular.module('xmpl.service', []).
2. value('greeter', {
3. salutation: 'Hello',
4. localize: function(localization) {
5. this.salutation = localization.salutation;
6. },
7. greet: function(name) {
8. return this.salutation + ' ' + name + '!';
9. }
10. }).
11. value('user', {
12. load: function(name) {
13. this.name = name;
14. }
15. });
16.
17. angular.module('xmpl.directive', []);
18.
19. angular.module('xmpl.filter', []);
20.
21. angular.module('xmpl', ['xmpl.service', 'xmpl.directive', 'xmpl.filter']).
22. run(function(greeter, user) {
23. // This is effectively part of the main method initialization code
24. greeter.localize({
25. salutation: 'Bonjour'
26. });
27. user.load('World');
28. })
29.
30.
31. // A Controller for your app
32. var XmplController = function($scope, greeter, user) {
33. $scope.greeting = greeter.greet(user.name);
34. }
,
.
:
1. .
.
, .
2. ,
. .
.
1. angular.module('myModule', []).
2. config(function(injectables) { // provider-injector
3. // .
4. // .
5. // ( )
6. // .
7. }).
8. run(function(injectables) { // instance-injector
9. // .
10. // .
11. // ( )
12. //
13. });
, . :
1. angular.module('myModule', []).
2. value('a', 123).
3. factory('a', function() { return 123; }).
4. directive('directiveName', ...).
5. filter('filterName', ...);
6.
7. //
8.
9. angular.module('myModule', []).
10. config(function($provide, $compileProvider, $filterProvider) {
11. $provide.value('a', 123);
12. $provide.factory('a', function() { return 123; });
13. $compileProvider.directive('directiveName', ...);
14. $filterProvider.register('filterName', ...);
15. });
, .
,
.
Angular .
. .
, -,
, -
.
, . ,
.
,
. .
, .
- $injector,
VM. , ,
Angular. , VM
, ,
.
. ,
. . ,
, , VM.
, :
1. angular.module('greetMod', []).
2.
3. factory('alert', function($window) {
4. return function(text) {
5. $window.alert(text);
6. }
7. }).
8.
9. value('salutation', 'Hello').
10.
11. factory('greet', function(alert, salutation) {
12. return function(name) {
13. alert(salutation + ' ' + name + '!');
14. }
15. });
1. describe('myApp', function() {
1. // ,
2. // $window mock ,
3. // $window.alert
4. // .
5. // .
6. beforeEach(module('greetMod', function($provide) {
7. $provide.value('$window', {
8. alert: jasmine.createSpy('alert')
9. });
10. }));
11.
12. // inject() injector greet
13. // $window . ,
14. // .
15. it('should alert on $window', inject(function(greet, $window) {
16. greet('World');
17. expect($window.alert).toHaveBeenCalledWith('Hello World!');
18. }));
19.
20. //
21. // .
22. it('should alert using the alert service', function() {
23. var alertSpy = jasmine.createSpy('alert');
24. module(function($provide) {
25. $provide.value('alert', alertSpy);
26. });
27. inject(function(greet) {
28. greet('World');
29. expect(alertSpy).toHaveBeenCalledWith('Hello World!');
30. });
31. });
32. });
?
, .
. , DOM
. .
($watch)
.
($apply)
, Angular " (,
, Angular ).
, ,
. ()
.
, . {{username}}
, ,
username.
,
.
$watch,
. $watch
, DOM.
, .
, DOM. ,
, .
:
Index.html
1. <!doctype html>
2. <html ng-app>
3. <head>
4. <script src="http://code.angularjs.org/1.0.6/angular.min.js"></script>
5. <script src="script.js"></script>
6. </head>
7. <body>
8. <div ng-controller="MyController">
9. Your name:
10. <input type="text" ng-model="username">
11. <button ng-click='sayHello()'>greet</button>
12. <hr>
13. {{greeting}}
14. </div>
15. </body>
16. </html>
Script.js
1. function MyController($scope) {
2. $scope.username = 'World';
3.
4. $scope.sayHello = function() {
5. $scope.greeting = 'Hello ' + $scope.username + '!';
6. };
7. }
, DOM , {{
greeting }}. , ,
MyController. ( .)
greeting ,
.
, ,
.
.
, , ..
, .
angular ,
.
,
(. , ,
). ,
. , DOM,
.
Angular {{username}}, ,
, . ,
, , .
JavaScript ,
.
, .
I ndex .htm l
1. <!doctype html>
2. <html ng-app>
3. <head>
4. <script src="http://code.angularjs.org/1.0.6/angular.min.js"></script>
5. <script src="script.js"></script>
6. </head>
7. <body>
8. <div ng-controller="EmployeeController">
9. Manager: {{employee.name}} [ {{department}} ]<br>
10. Reports:
11. <ul>
12. <li ng-repeat="employee in employee.reports">
13. {{employee.name}} [ {{department}} ]
14. </li>
15. </ul>
16. <hr>
17. {{greeting}}
18. </div>
19. </body>
20. </html>
Style.css
1. function EmployeeController($scope) {
2. $scope.department = 'Engineering';
3. $scope.employee = {
4. name: 'Joe the Manager',
5. reports: [
6. {name: 'John Smith'},
7. {name: 'Mary Run'}
8. ]
9. };
10. }
, Angular ng-scope ,
. <style>
. , ,
{{ employee.name }}
. {{
department }}, ,
, .
DOM.
DOM $scope, .
(,
.) , DOM,
ng-app. ng-app <html>,
, , , ,
Angular.
:
1.
" . .
2. ,
$0.
3. : angular.element
($0).scope().
DOM.
.
:
I ndex .htm l
1. <!doctype html>
2. <html ng-app>
3. <head>
4. <script src="http://code.angularjs.org/1.0.6/angular.min.js"></script>
5. <script src="script.js"></script>
6. </head>
7. <body>
8. <div ng-controller="EventController">
9. Root scope <tt>MyEvent</tt> count: {{count}}
10. <ul>
11. <li ng-repeat="i in [1]" ng-controller="EventController">
12. <button ng-click="$emit('MyEvent')">$emit('MyEvent')</button>
13. <button ng-click="$broadcast('MyEvent')">$broadcast('MyEvent')</button>
14. <br>
15. Middle scope <tt>MyEvent</tt> count: {{count}}
16. <ul>
17. <li ng-repeat="item in [1, 2]" ng-controller="EventController">
18. Leaf scope <tt>MyEvent</tt> count: {{count}}
19. </li>
20. </ul>
21. </li>
22. </ul>
23. </div>
24. </body>
25. </html>
Script.js
1. function EventController($scope) {
2. $scope.count = 0;
3. $scope.$on('MyEvent', function() {
4. $scope.count++;
5. });
6. }
JavaScript. DOM
.
Angular, Angular
. Angular
, $apply. ,
$apply Angular. ,
DOM, ng-click $apply.
, $apply $digest. $digest
$watch . .
, , $ scope.username = "angular"
$watch, , $watch
$digest. ,
$watch , , $watch
$watch . $watch ,
$digest.
1.
$injector.
.
2. Watcher
.
DOM.
3.
$aplly. (Angular API
, $aplly ,
a $http $timeout.
4.
$apply, Angular $digest ,
. $digest, $watch
, $watch
.
5.
, , API
$destroy. $digest
, .
, DOM.
:
, {{expression}},
$watch (). ,
.
, ng-click, DOM.
DOM,
$apply().
(, , XHR) ,
$apply(),
.
, , .
, , ng-controller ng-repeat,
DOM.
DOM angular.element(aDomElement).scope().
:
(.
ng-controller).
(), (
).
.
, .
. ng-controller .
$watch
Angular .
, DOM, .. DOM ,
JavaScript.
(DI) ,
.
DI Dependency Injection, Inversion of Control ,
DI .
DI
, :
1. , new.
2. .
3. , .
,
. , , .
,
.
,
. .
1. function SomeClass(greeter) {
2. this.greeter = greeter;
3. }
4.
5. SomeClass.prototype.doSomething = function(name) {
6. this.greeter.greet(name);
7. }
SomeClass , greeter,
.
, ,
SomeClass.
, Angular
. , .
:
1. //
2. angular.module('myModule', []).
3.
4. // 'greeter'
5. // greeter '$window'
6. factory('greeter', function($window) {
7. //
8. // 'greet'.
9. return {
10. greet: function(text) {
11. $window.alert(text);
12. }
13. };
14. });
15.
16. // .
17. // ( Angular)
18. var injector = angular.injector(['myModule', 'ng']);
19.
20. //
21. var greeter = injector.get('greeter');
, ,
. .
, ,
:
1. //
2. function MyController($scope, greeter) {
3. $scope.sayHello = function() {
4. greeter.greet('Hello World');
5. };
6. }
7.
8. // 'ng-controller'
9. injector.instantiate(MyController);
, ng-controller,
MyController , . .
, , .
.
, ?
,
. Angular, API
, API. ,
. .
.
,
.
. $scope greeter
, .
, JavaScript /
, .
-.
$inject
-
, $inject. $inject
.
, $inject ,
.
,
.
$inject, , .
:
1. someModule.factory('greeter', function($window) {
2. ...
3. });
, :
, ,
Angular .
DI?
DI Angular . .
DI
, .
:
Angular. ,
. , ,
:
1. angular.module('myModule', []).
2. config(['depProvider', function(depProvider){
3. ...
4. }]).
5. factory('serviceId', ['depService', function(depService) {
6. ...
7. }]).
8. directive('directiveName', ['depService', function(depService) {
9. ...
10. }]).
11. filter('filterName', ['depService', function(depService) {
12. ...
13. }]).
14. run(['depService', function(depService) {
15. ...
16. }]);
MVC Angular
-- (MVC) ,
, , Angular MVC
-.
MVC :
, ,
MVC Angular ,
.
, Angular MVC -
:
Angular
, (,
) ( ).
Angular ,
Angular. , , JavaScript
( ).
, JavaScript Angular,
Angular . .
,
JavaScript :
JavaScript-;
:
function MyCtrl($scope) {
$scope.foo = 'bar';
}
Angular :
ng-init (
).
Angular ( )
:
query fluffy
cloud.
ng-repeat
, Angular
.
Angular ( JavaScript function#apply)
, . , Angular
( new
). .
. :
function GreetingCtrl($scope) { $scope.greeting = 'Hola!'; }
GreetingCtrl greeting, .
:
. -
Angular controller() , :
,
$scope, Angular.
Angular ,
.
.
, ( ),
. ,
Angular
, (, ng-click).
, . -
.
,
, ,
.
.
:
DOM -.
DOM .
-.
Angular DOM.
DOM, .
Angular .
Angular .
, Angular.
(,
).
Angular
, API
scope.$new(), ngController $route.
, Angular ,
:
, spice
, spice
spice,
very. , , spice chili
jalapeo, .
Spicy
1. <body ng-controller="SpicyCtrl">
2. <button ng-click="chiliSpicy()">Chili</button>
3. <button ng-click="jalapenoSpicy()">Jalapeo</button>
4. <p>The food is {{spice}} spicy!</p>
5. </body>
6.
7. function SpicyCtrl($scope) {
8. $scope.spice = 'very';
9. $scope.chiliSpicy = function() {
10. $scope.spice = 'chili';
11. }
12. $scope.jalapenoSpicy = function() {
13. $scope.spice = 'jalapeo';
14. }
15. }
:
NgController ()
, () SpicyCtrl .
SpicyCtrl JavaScript. ()
Ctrl Controller.
$scope .
(
chiliSpicy)
( body ).
NB: Angular (pre 1.0 RC) this $scope,
. , this $scope
(Angular this = $scope),
.
NB: Angular(pre 1.0 RC)
, ;
.
,
.
1. <body ng-controller="SpicyCtrl">
2. <input ng-model="customSpice" value="wasabi">
3. <button ng-click="spicy('chili')">Chili</button>
4. <button ng-click="spicy(customSpice)">Custom spice</button>
5. <p>The food is {{spice}} spicy!</p>
6. </body>
7.
8. function SpicyCtrl($scope) {
9. $scope.spice = 'very';
10. $scope.spicy = function(spice) {
11. $scope.spice = spice;
12. }
13. }
, SpicyCtrl spicy,
spice.
'chili'
customSpice ( ) .
Angular .
:
1. <body ng-controller="MainCtrl">
2. <p>Good {{timeOfDay}}, {{name}}!</p>
3. <div ng-controller="ChildCtrl">
4. <p>Good {{timeOfDay}}, {{name}}!</p>
5. <p ng-controller="BabyCtrl">Good {{timeOfDay}}, {{name}}!</p>
6. </div>
7. </body>
8.
9. function MainCtrl($scope) {
10. $scope.timeOfDay = 'morning';
11. $scope.name = 'Nikki';
12. }
13.
14. function ChildCtrl($scope) {
15. $scope.name = 'Mattie';
16. }
17.
18. function BabyCtrl($scope) {
19. $scope.timeOfDay = 'evening';
20. $scope.name = 'Gingerbreak Baby';
21. }
ngController .
4 :
MainCtrl, timeOfDay name
ChildCtrl, name
timeOfDay.
BabyCtrl, , timeOfDay
MainCtrl name, ChildCtrl
, .
, , ,
.
: ,
, , , Angular,
.
, , ,
$rootScope $controller.
1. function myController($scope) {
2. $scope.spices = [{"name":"pasilla", "spiciness":"mild"},
3. {"name":"jalapeno", "spiceiness":"hot hot hot!"},
4. {"name":"habanero", "spiceness":"LAVA HOT!!"}];
5.
6. $scope.spice = "habanero";
7. }
,
, , DOM.
1. describe('state', function() {
2. var mainScope, childScope, babyScope;
3.
4. beforeEach(inject(function($rootScope, $controller) {
5. mainScope = $rootScope.$new();
6. var mainCtrl = $controller(MainCtrl, {$scope: mainScope});
7. childScope = mainScope.$new();
8. var childCtrl = $controller(ChildCtrl, {$scope: childScope});
9. babyScope = childCtrl.$new();
10. var babyCtrl = $controller(BabyCtrl, {$scope: babyScope});
11. }));
12.
13. it('should have over and selected', function() {
14. expect(mainScope.timeOfDay).toBe('morning');
15. expect(mainScope.name).toBe('Nikki');
16. expect(childScope.timeOfDay).toBe('morning');
17. expect(childScope.name).toBe('Mattie');
18. expect(babyScope.timeOfDay).toBe('evening');
19. expect(babyScope.name).toBe('Gingerbreak Baby');
20. });
21. });
Angular DOM,
, , .
Angular MVC, .
.
Angular , ngController ngView,
: {{someControllerFunction()}}. ,
.
E2e
,
, .
, Angular ,
, Angular.
JavaScript, , ,
.
( ), ,
. , (,
), ,
(, URL-).
, failed .
beforeEach afterEach , ( ) , ,
.
, it.
API
pause()
resume() ( resume
Runner UI).
sleep(seconds)
seconds .
browser().navigateTo(url)
url .
browser().navigateTo(url, fn)
URL fn .
url . URL
( ).
browser().reload()
.
browser().window().href()
window.location.href , .
browser().window().path()
window.location.pathname , .
browser().window().search()
window.location.search , .
browser().window().hash()
window.location.hash , .
browser().location().url()
$location.url() , .
browser().location().path()
$location.path() , .
browser().location().search()
$location.search() , .
browser().location().hash()
$location.hash() , .
expect(future).{matcher}
, future matcher. API
future, value
. angular.scenario.matcher,
. :
expect(browser().location().href()).toEqual('http://www.google.com')
expect(future).not().{matcher}
, future matcher.
using(selector, label)
DSL .
binding(name)
name.
input(name).enter(value)
value name.
input(name).check()
/ name.
input(name).select(value)
value name.
input(name).val()
name.
repeater(selector, label).count()
, jQuery
selector. label .
repeater(selector, label).row(index)
index
jQuery selector. label .
repeater(selector, label).column(binding)
binding
jQuery selector. label .
select(name).option(value)
, value select name.
select(name).option(value1, value2...)
, values select name.
element(selector, label).count()
, jQuery selector.
label .
element(selector, label).click()
, jQuery selector. label
.
element(selector, label).query(fn)
fn(selectedElements, done), selectedElements
jQuery selector done , ,
fn. label .
element(selector, label).{method}()
method , jQuery
selector, method jQuery
: val, text, html, height, innerHeight, outerHeight, width, innerWidth,outerWidt
h, position, scrollLeft, scrollTop, offset. label .
element(selector, label).{method}(value)
method value ,
jQuery selector, method
jQuery: val, text, html, height, innerHeight, outerHeight, width,innerWidth, outerWidth,
position, scrollLeft, scrollTop, offset. label .
element(selector, label).{method}(key)
method
key , jQuery selector, method
jQuery : attr, prop, css. label .
Angular
Angular ,
, .
DOM, HTML, CSS, angular. Angular
angular
DOM DOM.
Angular :
Directive DOM
DOM .
Markup ,
Angular.
Filter .
Form controls .
: ,
JavaScript .
Angular HTML
Angular :
1. <html ng-app>
2. <! Body ngController-->
3. <body ng-controller="MyController">
4. <input ng-model="foo" value="bar">
5. <! Button ng-click,
6. 'buttonText'
7. "{{ }}" markup -->
8. <button ng-click="changeFoo()">{{buttonText}}</button>
9. <script src="angular.js">
10. </body>
11. </html>
CSS Angular
Angular CSS . .
: angular ,
. ( input ).
ng-pristine, ng-dirty
- Angular
. Angular
, .
. ,
.
:
,
. ,
.
,
,
. ,
.
Angular
Angular , . ,
(
HTML
)
,
.
,
.
,
.
.
,
. , ..
DOM/ .
Angular .
, ,
. ,
, :
name | uppercase
name uppercase .
Angular
:
, .
, .
.
.
.
Index.html
1. <!doctype html>
2. <html ng-app="MyReverseModule">
3. <head>
4. <script src="http://code.angularjs.org/1.0.6/angular.min.js"></script>
5. <script src="script.js"></script>
6. </head>
7. <body>
8. <div ng-controller="Ctrl">
9. <input ng-model="greeting" type="greeting"><br>
10. No filter: {{greeting}}<br>
11. Reverse: {{greeting|reverse}}<br>
12. Reverse + uppercase: {{greeting|reverse:true}}<br>
13. </div>
14. </body>
15. </html>
Script.js
1. angular.module('MyReverseModule', []).
2. filter('reverse', function() {
3. return function(input, uppercase) {
4. var out = "";
5. for (var i = 0; i < input.length; i++) {
6. out = input.charAt(i) + out;
7. }
8. // conditional based on optional argument
9. if (uppercase) {
10. out = out.toUpperCase();
11. }
12. return out;
13. }
14. });
15.
16. function Ctrl($scope) {
17. $scope.greeting = 'hello';
18. }
Angular
api/ng.$rootScope.Scope,
:
{{ expression | filter }}
,
. , .
, :
{{ expression | filter1 | filter2 }}
: , ,
123 :
123 | number:2
:
myArray | orderBy:'timestamp':true
,
:
Angular
, - ,
. apps
, (DI).
, Angular ,
. Angular
,
(DI).
$location
?
$location URL ( window.location)
URL . URL
$location $location .
$location:
URL ,
URL.
URL.
URL
( ).
.
URL (protocol, host, port, path, search, hash).
$location window.location
window.location $location service
API "raw"
, jQuery
,
angular $watch, ...
(
HTML5 API )
- window.location.path - $location.path()
/ "/docroot/actual/path" "/actual/path"
$location?
URL
URL .
?
.
URL, API
$window.location.href.
API
$location , ,
. ,
.
$location,
jQuery, URL
.
$location
$location, $locationProvider
:
html5Mode(mode): {boolean}
true HTML5
false
default: false
hashPrefix(prefix): {string}
URLs ( ,
HTML5)
default: ""
1. $locationProvider.html5Mode(true).hashPrefix('!');
$location URL (absUrl, protocol, host,
port) / url, path, search, hash:
1. //
2. $location.path();
3.
4. //
5. $location.path('/newValue')
$location . ,
, :
1. $location.path('/newValue').search({key: value});
replace $location
,
, .
,
( ).
URL :
1. $location.path('/someNewPath');
2. $location.replace();
3. // or you can chain these as: $location.path('/someNewPath').replace();
window.location. ,
$location
$location window.location
$digest . $location ,
replace(),
. , $location
replace() ,
replace().
. $location
RFC 3986. :
, $location,
path(), search(), hash(), .
( )
path(), search(),hash().
absUrl() url
.
url(), , ,
/path?search=a&b=c#hash. .
HTML5
$location , URL
: ( ) HTML5
History API. API
$location URL APIs
URL .
HTML5
{ html5Mode: true }
<a href="">
( )
, $location URLs
AJAX ,
- head :
, _escaped_fragment_ param
HTML.
AJAX .
HTML5
HTML5, $location, ,
URL HTML5 API, URL
, . HTML5 API
, $location .
, API ;
$location .
HTML5 history API, $location
. history API , $location
URL. history API;
$location .
Html
HTML5 history API,
, , URL ,
: <a href="/some?foo=bar">link</a>
URL /index.html#!/some?foo=bar
URL /some?foo=bar
;
.
target
: <a href="/ext/link?a=b" target="_self">link</a>
,
: <a href="http://angularjs.org/">link</a>
'/'
: <a href="/not-my-base/link">link</a>
URL ,
( index.html)
, , .. url
head (<base href="/my-base">)
url ( /)
.
HTML5,
:
URL URL.
URL URL .
$location, Html5,
, .
$location . input
.
, url /
/hashbang url, URL =
.
<base href="/base/index.html" />
Index.html
1. <!doctype html>
2. <html ng-app>
3. <head>
4. <script src="http://code.angularjs.org/1.0.6/angular.min.js"></script>
5. <script src="script.js"></script>
6. </head>
7. <body>
8. <div ng-non-bindable class="html5-hashbang-example">
9. <div id="html5-mode" ng-controller="Html5Cntl">
10. <h4>Browser with History API</h4>
11. <div ng-address-bar browser="html5"></div><br><br>
12. $location.protocol() = {{$location.protocol()}}<br>
13. $location.host() = {{$location.host()}}<br>
14. $location.port() = {{$location.port()}}<br>
15. $location.path() = {{$location.path()}}<br>
16. $location.search() = {{$location.search()}}<br>
17. $location.hash() = {{$location.hash()}}<br>
18. <a href="http://www.host.com/base/first?a=b">/base/first?a=b</a> |
19. <a href="http://www.host.com/base/sec/ond?flag#hash">sec/ond?flag#hash</a>
|
20. <a href="/other-base/another?search">external</a>
21. </div>
22.
23. <div id="hashbang-mode" ng-controller="HashbangCntl">
24. <h4>Browser without History API</h4>
25. <div ng-address-bar browser="hashbang"></div><br><br>
26. $location.protocol() = {{$location.protocol()}}<br>
27. $location.host() = {{$location.host()}}<br>
28. $location.port() = {{$location.port()}}<br>
29. $location.path() = {{$location.path()}}<br>
30. $location.search() = {{$location.search()}}<br>
31. $location.hash() = {{$location.hash()}}<br>
32. <a href="http://www.host.com/base/first?a=b">/base/first?a=b</a> |
33. <a href="http://www.host.com/base/sec/ond?flag#hash">sec/ond?flag#hash</a>
|
34. <a href="/other-base/another?search">external</a>
35. </div>
36. </div>
37. </body>
38. </html>
Script.js
$location URL; .
URL ,
API, $window.location.href.
$location
$location Angular. URL
, $location $apply
$watchers / $observers. $location $digest
; $location
$watchers / $observers. $location Angular ( ,
DOM ) $apply
.
$location.path() ! /
(/); $location.path()
.
, ! $location.path();
.
$location
$location ,
. scope.$apply().
1. describe('serviceUnderTest', function() {
2. beforeEach(module(function($provide) {
3. $provide.factory('serviceUnderTest', function($location){
4. // whatever it does...
5. });
6. });
7.
8. it('should...', inject(function($location, $rootScope, serviceUnderTest) {
9. $location.path('/new/path');
10. $rootScope.$apply();
11.
12. // test whatever the service should do...
13.
14. }));
15. });
AngularJS
Angular, $location hashPath hashSearch
. , $location
path search
URL (
http://server.com/#!/path?search=a), .
$location.update(value)
$location.updateHash(value)
API
$location.hashPath $location.path()
$location.hashSearch $location.search()
$location.href $location.absUrl()
$location.protocol $location.protocol()
$location.host $location.host()
$location.port $location.port()
$location.hash $location.path() + $location.search()
$location.path $window.location.path
$location.search $window.location.search
$location
Angular
(. ). $location (
ngModel ),
( locationPath) , $location
. :
1. // js - controller
2. $scope.$watch('locationPath', function(path) {
3. $location.path(path);
4. });
5.
6. $scope.$watch('$location.path()', function(path) {
7. scope.locationPath = path;
8. });
Angular ,
. ,
Module#factory api
, $provide.
Angular (DI),
, ,
. / /
.
, , .
Module api $provide
. :
angular.Module api:
$provide:
, , ,
, .
, .
. (DI)
Angular. , $inject ,
.
. $window (
) .
, window alert.
Angular
Angular . ,
. ,
Angular ,
.
, Angular .
. Angular
, ,
, , .
JavaScript , DI
( ).
, $inject , ,
. ID ,
. .
, ,
.
index.html
1. <!doctype html>
2. <html ng-app="MyServiceModule">
3. <head>
4. <script src="http://code.angularjs.org/1.0.6/angular.min.js"></script>
5. <script src="script.js"></script>
6. </head>
7. <body>
8. <div ng-controller="myController">
9. <p>Let's try this simple notify service, injected into the controller...</p>
10. <input ng-init="message='test'" ng-model="message" >
11. <button ng-click="callNotify(message);">NOTIFY</button>
12. </div>
13. </body>
14. </html>
script.js
1. angular.
2. module('MyServiceModule', []).
3. factory('notify', ['$window', function(win) {
4. var msgs = [];
5. return function(msg) {
6. msgs.push(msg);
7. if (msgs.length == 3) {
8. win.alert(msgs.join("\n"));
9. msgs = [];
10. }
11. };
12. }]);
13.
14. function myController(scope, notifyService) {
15. scope.callNotify = function(msg) {
16. notifyService(msg);
17. };
18. }
19.
20. myController.$inject = ['$scope','notify'];
Angular DI .
$window, $scope notify:
Index.html
1. <!doctype html>
2. <html ng-app="MyServiceModuleDI">
3. <head>
4. <script src="http://code.angularjs.org/1.0.6/angular.min.js"></script>
5. <script src="script.js"></script>
6. </head>
7. <body>
8. <div ng-controller="myController">
9. <p>Let's try the notify service, that is implicitly injected into the contro
ller...</p>
10. <input ng-init="message='test'" ng-model="message">
11. <button ng-click="callNotify(message);">NOTIFY</button>
12. </div>
13. </body>
14. </html>
Script.js
1. angular.
2. module('MyServiceModuleDI', []).
3. factory('notify', function($window) {
4. var msgs = [];
5. return function(msg) {
6. msgs.push(msg);
7. if (msgs.length == 3) {
8. $window.alert(msgs.join("\n"));
9. msgs = [];
10. }
11. };
12. });
13.
14. function myController($scope, notify) {
15. $scope.callNotify = function(msg) {
16. notify(msg);
17. };
18. }
, , ,
$inject.
Angular ,
.
, .
, $inject .
, $inject .
1. function myModuleCfgFn($provide) {
2. $provide.factory('myService', ['dep1', 'dep2', function(dep1, dep2) {}]);
3. }
$inject:
1. function myModuleCfgFn($provide) {
2. var myServiceFactory = function(dep1, dep2) {};
3. myServiceFactory.$inject = ['dep1', 'dep2'];
4. $provide.factory('myService', myServiceFactory);
5. }
DI ( ):
1. function myModuleCfgFn($provide) {
2. $provide.factory('myService', function(dep1, dep2) {});
3. }
, ,
Angular:
1. /**
2. * batchLog service allows for messages to be queued in memory and flushed
3. * to the console.log every 50 seconds.
4. *
5. * @param {*} message Message to be logged.
6. */
7. function batchLogModule($provide){
8. $provide.factory('batchLog', ['$timeout', '$log', function($timeout, $log) {
9. var messageQueue = [];
10.
11. function log() {
12. if (messageQueue.length) {
13. $log('batchLog messages: ', messageQueue);
14. messageQueue = [];
15. }
16. $timeout(log, 50000);
17. }
18.
19. // start periodic checking
20. log();
21.
22. return function(message) {
23. messageQueue.push(message);
24. }
25. }]);
26.
27. /**
28. * routeTemplateMonitor monitors each $route change and logs the current
29. * template via the batchLog service.
30. */
31. $provide.factory('routeTemplateMonitor',
32. ['$route', 'batchLog', '$rootScope',
33. function($route, batchLog, $rootScope) {
34. $rootScope.$on('$routeChangeSuccess', function() {
35. batchLog($route.current ? $route.current.template : null);
36. });
37. }]);
38. }
39.
40. // get the main service to kick of the application
41. angular.injector([batchLogModule]).get('routeTemplateMonitor');
Angular
'notify' 'Dependencies' Angular.
Jasmine spy (mock) alert.
Angular
Angular , -,
$http service
XMLHttpRequest.
Angular,
( ). Angular
. Angular ,
, .
Angular "constructor" (
). JavaScript , Angular
.
$inject. :
myController.$inject = ['$location'];
Angular - .
Angular, ,
$ ( , $http).
.
JavaScript , ,
. ,
JavaScript . Angular ,
Angular.
.
, .
, " ?"
" ?"
, ,
. , ,
, DOM XHR .
,
. , ,
. XHR, , DOM.
Angular ,
XHR ( )
,
DOM. ,
, , ,
.
Angular ,
. , ,
.
: 1.
new. 2. , . 3.
( , .
2). 4. .
, . :
new
, new ,
, . , ,
, XHR.
1. function MyClass() {
2. this.doWork = function() {
3. var xhr = new XHR();
4. xhr.open(method, url, true);
5. xhr.onreadystatechange = function() {...}
6. xhr.send();
7. }
8. }
,
MockXHR .
new XHR() XHR,
. , , ,
.
, :
:
, .
1. function MyClass() {
2. this.doWork = function() {
3. global.xhr({
4. method:'...',
5. url:'...',
6. complete:function(response){ ... }
7. })
8. }
9. }
, ,
new, global.xhr
, . , ,
.
, , :
, :
:
, ,
.
1. function MyClass() {
2. var serviceRegistry = ????;
3. this.doWork = function() {
4. var xhr = serviceRegistry.get('xhr');
5. xhr({
6. method:'...',
7. url:'...',
8. complete:function(response){ ... }
9. })
10. }
, serviceRegistry? : * new ,
* ,
( , ).
, :
:
, .
1. function MyClass(xhr) {
2. this.doWork = function() {
3. xhr({
4. method:'...',
5. url:'...',
6. complete:function(response){ ... }
7. })
8. }
, ,
xhr , . ,
, ,
.
, :
, .
Angular
, .
, .. ,
. DOM,
, :
1. function PasswordCtrl() {
2. // get references to DOM elements
3. var msg = $('.ex1 span');
4. var input = $('.ex1 input');
5. var strength;
6.
7. this.grade = function() {
8. msg.removeClass(strength);
9. var pwd = input.val();
10. password.text(pwd);
11. if (pwd.length > 8) {
12. strength = 'strong';
13. } else if (pwd.length > 3) {
14. strength = 'medium';
15. } else {
16. strength = 'weak';
17. }
18. msg
19. .addClass(strength)
20. .text(strength);
21. }
22. }
,
DOM, , .
:
angular DOM,
, ::
1. function PasswordCtrl($scope) {
2. $scope.password = '';
3. $scope.grade = function() {
4. var size = $scope.password.length;
5. if (size > 8) {
6. $scope.strength = 'strong';
7. } else if (size > 3) {
8. $scope.strength = 'medium';
9. } else {
10. $scope.strength = 'weak';
11. }
12. };
13. }
, , .
, , ,
.
,
. ,
, .
1. myModule.filter('length', function() {
2. return function(text){
3. return (''+(text||'')).length;
4. }
5. });
6.
7. var length = $filter('length');
8. expect(length(null)).toEqual(0);
9. expect(length('abc')).toEqual(3);
angular
HTML, , .
, , ,
.
HTML
angular .
1. app.directive('aGreatEye', function () {
2. return {
3. restrict: 'E',
4. replace: true,
5. template: '<h1>lidless, wreathed in flame</h1>'
6. };
7. });
<a-great-eye></a-great-eye>.
<h1>lidless, wreathed in flame</h1>. jasmine
.
Mocks
oue
JavaScriptTestDriver
ou
Jasmine
ou
Sample project
uoe