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 /src/directives.js | |
| 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 'src/directives.js')
| -rw-r--r-- | src/directives.js | 69 |
1 files changed, 35 insertions, 34 deletions
diff --git a/src/directives.js b/src/directives.js index 1d5b36f2..53d03573 100644 --- a/src/directives.js +++ b/src/directives.js @@ -97,28 +97,30 @@ angularDirective("ng:init", function(expression){ <doc:example> <doc:source> <script type="text/javascript"> - function SettingsController() { - this.name = "John Smith"; - this.contacts = [ + function SettingsController($scope) { + $scope.name = "John Smith"; + $scope.contacts = [ {type:'phone', value:'408 555 1212'}, {type:'email', value:'john.smith@example.org'} ]; - } - SettingsController.prototype = { - greet: function() { + + $scope.greet = function() { alert(this.name); - }, - addContact: function() { + }; + + $scope.addContact = function() { this.contacts.push({type:'email', value:'yourname@example.org'}); - }, - removeContact: function(contactToRemove) { + }; + + $scope.removeContact = function(contactToRemove) { var index = this.contacts.indexOf(contactToRemove); this.contacts.splice(index, 1); - }, - clearContact: function(contact) { + }; + + $scope.clearContact = function(contact) { contact.type = 'phone'; contact.value = ''; - } - }; + }; + } </script> <div ng:controller="SettingsController"> Name: <input type="text" ng:model="name"/> @@ -156,16 +158,15 @@ angularDirective("ng:init", function(expression){ </doc:scenario> </doc:example> */ -angularDirective("ng:controller", function(expression){ - this.scope(function(scope){ - var Controller = - getter(scope, expression, true) || - getter(window, expression, true); +angularDirective("ng:controller", function(expression) { + this.scope(true); + return ['$injector', '$window', function($injector, $window) { + var scope = this, + Controller = getter(scope, expression, true) || getter($window, expression, true); + assertArgFn(Controller, expression); - inferInjectionArgs(Controller); - return Controller; - }); - return noop; + $injector.instantiate(Controller, {$scope: scope}); + }]; }); /** @@ -189,8 +190,8 @@ angularDirective("ng:controller", function(expression){ <doc:example> <doc:source> <script> - function Ctrl() { - this.name = 'Whirled'; + function Ctrl($scope) { + $scope.name = 'Whirled'; } </script> <div ng:controller="Ctrl"> @@ -277,9 +278,9 @@ angularDirective("ng:bind", function(expression, element){ <doc:example> <doc:source> <script> - function Ctrl() { - this.salutation = 'Hello'; - this.name = 'World'; + function Ctrl($scope) { + $scope.salutation = 'Hello'; + $scope.name = 'World'; } </script> <div ng:controller="Ctrl"> @@ -363,8 +364,8 @@ angularDirective("ng:bind-template", function(expression, element){ <doc:example> <doc:source> <script> - function Ctrl() { - this.query = 'AngularJS'; + function Ctrl($scope) { + $scope.query = 'AngularJS'; } </script> <div ng:controller="Ctrl"> @@ -470,10 +471,10 @@ angularDirective("ng:click", function(expression, element){ <doc:example> <doc:source> <script> - function Ctrl() { - this.list = []; - this.text = 'hello'; - this.submit = function() { + function Ctrl($scope) { + $scope.list = []; + $scope.text = 'hello'; + $scope.submit = function() { if (this.text) { this.list.push(this.text); this.text = ''; |
