diff options
| author | Vojta Jina | 2011-11-29 21:51:59 -0800 |
|---|---|---|
| committer | Vojta Jina | 2012-01-23 11:05:36 -0800 |
| commit | 992c790f0786fa45c1cc3710f29bf49c7c322ba7 (patch) | |
| tree | 581d06ea9ba275a14d5891d83b2df03f9930bd45 /docs/content/guide/dev_guide.forms.ngdoc | |
| parent | f5343c9fd3c7cd0fefdb4d71d2b579dbae998d6a (diff) | |
| download | angular.js-992c790f0786fa45c1cc3710f29bf49c7c322ba7.tar.bz2 | |
refactor(scope): separate controller from scope
Controller is standalone object, created using "new" operator, not messed up with scope anymore.
Instead, related scope is injected as $scope.
See design proposal: https://docs.google.com/document/pub?id=1SsgVj17ec6tnZEX3ugsvg0rVVR11wTso5Md-RdEmC0k
Closes #321
Closes #425
Breaks controller methods are not exported to scope automatically
Breaks Scope#$new() does not take controller as argument anymore
Diffstat (limited to 'docs/content/guide/dev_guide.forms.ngdoc')
| -rw-r--r-- | docs/content/guide/dev_guide.forms.ngdoc | 72 |
1 files changed, 34 insertions, 38 deletions
diff --git a/docs/content/guide/dev_guide.forms.ngdoc b/docs/content/guide/dev_guide.forms.ngdoc index b4e37abd..03d0e6f9 100644 --- a/docs/content/guide/dev_guide.forms.ngdoc +++ b/docs/content/guide/dev_guide.forms.ngdoc @@ -111,10 +111,10 @@ The following example demonstrates: .ng-form {display: block;} </style> <script> - function UserFormCntl() { - this.state = /^\w\w$/; - this.zip = /^\d\d\d\d\d$/; - this.master = { + function UserFormCntl($scope) { + $scope.state = /^\w\w$/; + $scope.zip = /^\d\d\d\d\d$/; + $scope.master = { customer: 'John Smith', address:{ line1: '123 Main St.', @@ -123,28 +123,26 @@ The following example demonstrates: zip:'12345' } }; - this.cancel(); - } - UserFormCntl.prototype = { - cancel: function() { - this.form = angular.copy(this.master); - }, + $scope.cancel = function() { + $scope.form = angular.copy($scope.master); + }; - save: function() { - this.master = this.form; - this.cancel(); - }, + $scope.save = function() { + $scope.master = $scope.form; + $scope.cancel(); + }; - isCancelDisabled: function() { - return angular.equals(this.master, this.form); - }, + $scope.isCancelDisabled = function() { + return angular.equals($scope.master, $scope.form); + }; - isSaveDisabled: function() { - return this.userForm.$invalid || angular.equals(this.master, this.form); - } + $scope.isSaveDisabled = function() { + return $scope.userForm.$invalid || angular.equals($scope.master, $scope.form); + }; - }; + $scope.cancel(); + } </script> <div ng:controller="UserFormCntl"> @@ -282,15 +280,13 @@ This example shows how to implement a custom HTML editor widget in Angular. <doc:example> <doc:source> <script> - function EditorCntl() { - this.htmlContent = '<b>Hello</b> <i>World</i>!'; + function EditorCntl($scope) { + $scope.htmlContent = '<b>Hello</b> <i>World</i>!'; } - HTMLEditorWidget.$inject = ['$element', 'htmlFilter']; - function HTMLEditorWidget(element, htmlFilter) { - var self = this; - - this.$parseModel = function() { + HTMLEditorWidget.$inject = ['$element', '$scope', 'htmlFilter']; + function HTMLEditorWidget(element, scope, htmlFilter) { + scope.$parseModel = function() { // need to protect for script injection try { this.$viewValue = htmlFilter( @@ -305,13 +301,13 @@ This example shows how to implement a custom HTML editor widget in Angular. } } - this.$render = function() { + scope.$render = function() { element.html(this.$viewValue); } element.bind('keyup', function() { - self.$apply(function() { - self.$emit('$viewChange', element.html()); + scope.$apply(function() { + scope.$emit('$viewChange', element.html()); }); }); } @@ -364,13 +360,13 @@ validation. <doc:example> <doc:source> <script> - function Ctrl() { - this.input1 = ''; - this.input2 = ''; - this.input3 = 'A'; - this.input4 = false; - this.input5 = 'c'; - this.input6 = []; + function Ctrl($scope) { + $scope.input1 = ''; + $scope.input2 = ''; + $scope.input3 = 'A'; + $scope.input4 = false; + $scope.input5 = 'c'; + $scope.input6 = []; } </script> <table style="font-size:.9em;" ng:controller="Ctrl"> |
