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 | |
| 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')
| -rw-r--r-- | src/directives.js | 69 | ||||
| -rw-r--r-- | src/scenario/Runner.js | 11 | ||||
| -rw-r--r-- | src/service/compiler.js | 2 | ||||
| -rw-r--r-- | src/service/filter/filters.js | 16 | ||||
| -rw-r--r-- | src/service/filter/limitTo.js | 6 | ||||
| -rw-r--r-- | src/service/filter/orderBy.js | 6 | ||||
| -rw-r--r-- | src/service/formFactory.js | 41 | ||||
| -rw-r--r-- | src/service/http.js | 29 | ||||
| -rw-r--r-- | src/service/route.js | 10 | ||||
| -rw-r--r-- | src/service/scope.js | 19 | ||||
| -rw-r--r-- | src/widget/form.js | 4 | ||||
| -rw-r--r-- | src/widget/input.js | 89 | ||||
| -rw-r--r-- | src/widget/select.js | 38 | ||||
| -rw-r--r-- | src/widgets.js | 20 | 
14 files changed, 178 insertions, 182 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 = ''; diff --git a/src/scenario/Runner.js b/src/scenario/Runner.js index cfde1f64..06ad3aa1 100644 --- a/src/scenario/Runner.js +++ b/src/scenario/Runner.js @@ -152,7 +152,16 @@ angular.scenario.Runner.prototype.afterEach = function(body) {   * @param {Object} scope parent scope   */  angular.scenario.Runner.prototype.createSpecRunner_ = function(scope) { -  return scope.$new(angular.scenario.SpecRunner); +  var child = scope.$new(); +  var Cls = angular.scenario.SpecRunner; + +  // Export all the methods to child scope manually as now we don't mess controllers with scopes +  // TODO(vojta): refactor scenario runner so that these objects are not tightly coupled as current +  for (var name in Cls.prototype) +    child[name] = angular.bind(child, Cls.prototype[name]); + +  Cls.call(child); +  return child;  };  /** diff --git a/src/service/compiler.js b/src/service/compiler.js index 727f7983..adf1ffa9 100644 --- a/src/service/compiler.js +++ b/src/service/compiler.js @@ -22,7 +22,7 @@ function $CompileProvider(){            var childScope = scope,                locals = {$element: element};            if (this.newScope) { -            childScope = isFunction(this.newScope) ? scope.$new(this.newScope(scope)) : scope.$new(); +            childScope = scope.$new();              element.data($$scope, childScope);            }            forEach(this.linkFns, function(fn) { diff --git a/src/service/filter/filters.js b/src/service/filter/filters.js index 3e7f8e37..69bfbacf 100644 --- a/src/service/filter/filters.js +++ b/src/service/filter/filters.js @@ -18,8 +18,8 @@     <doc:example>       <doc:source>         <script> -         function Ctrl() { -           this.amount = 1234.56; +         function Ctrl($scope) { +           $scope.amount = 1234.56;           }         </script>         <div ng:controller="Ctrl"> @@ -69,8 +69,8 @@ function currencyFilter($locale) {     <doc:example>       <doc:source>         <script> -         function Ctrl() { -           this.val = 1234.56789; +         function Ctrl($scope) { +           $scope.val = 1234.56789;           }         </script>         <div ng:controller="Ctrl"> @@ -448,8 +448,8 @@ var uppercaseFilter = valueFn(uppercase);     <doc:example>       <doc:source>         <script> -         function Ctrl() { -           this.snippet = +         function Ctrl($scope) { +           $scope.snippet =               '<p style="color:blue">an html\n' +               '<em onmouseover="this.textContent=\'PWN3D!\'">click here</em>\n' +               'snippet</p>'; @@ -538,8 +538,8 @@ function htmlFilter() {     <doc:example>       <doc:source>         <script> -         function Ctrl() { -           this.snippet = +         function Ctrl($scope) { +           $scope.snippet =               'Pretty text with some links:\n'+               'http://angularjs.org/,\n'+               'mailto:us@somewhere.org,\n'+ diff --git a/src/service/filter/limitTo.js b/src/service/filter/limitTo.js index a250bd3b..eb97fdad 100644 --- a/src/service/filter/limitTo.js +++ b/src/service/filter/limitTo.js @@ -25,9 +25,9 @@     <doc:example>       <doc:source>         <script> -         function Ctrl() { -           this.numbers = [1,2,3,4,5,6,7,8,9]; -           this.limit = 3; +         function Ctrl($scope) { +           $scope.numbers = [1,2,3,4,5,6,7,8,9]; +           $scope.limit = 3;           }         </script>         <div ng:controller="Ctrl"> diff --git a/src/service/filter/orderBy.js b/src/service/filter/orderBy.js index 2e5a0286..c67d2769 100644 --- a/src/service/filter/orderBy.js +++ b/src/service/filter/orderBy.js @@ -32,14 +32,14 @@     <doc:example>       <doc:source>         <script> -         function Ctrl() { -           this.friends = +         function Ctrl($scope) { +           $scope.friends =                 [{name:'John', phone:'555-1212', age:10},                  {name:'Mary', phone:'555-9876', age:19},                  {name:'Mike', phone:'555-4321', age:21},                  {name:'Adam', phone:'555-5678', age:35},                  {name:'Julie', phone:'555-8765', age:29}] -           this.predicate = '-age'; +           $scope.predicate = '-age';           }         </script>         <div ng:controller="Ctrl"> diff --git a/src/service/formFactory.js b/src/service/formFactory.js index 15a4733f..565b22a4 100644 --- a/src/service/formFactory.js +++ b/src/service/formFactory.js @@ -25,15 +25,13 @@      <doc:example>        <doc:source>          <script> -          function EditorCntl() { -            this.html = '<b>Hello</b> <i>World</i>!'; +          function EditorCntl($scope) { +            $scope.html = '<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(this.$modelValue || '').get(); @@ -47,13 +45,13 @@                }              } -            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());                });              });            } @@ -104,7 +102,8 @@  function $FormFactoryProvider() {    var $parse; -  this.$get = ['$rootScope', '$parse',  function($rootScope, $parse_) { +  this.$get = ['$rootScope', '$parse', '$injector', +      function($rootScope, $parse_, $injector) {      $parse = $parse_;      /**       * @ngdoc proprety @@ -136,7 +135,9 @@ function $FormFactoryProvider() {      return formFactory;      function formFactory(parent) { -      return (parent || formFactory.rootForm).$new(FormController); +      var scope = (parent || formFactory.rootForm).$new(); +      $injector.instantiate(FormController, {$scope: scope}); +      return scope;      }    }]; @@ -230,8 +231,11 @@ function $FormFactoryProvider() {     * @param {*} viewValue The new value for the view which will be assigned to `widget.$viewValue`.     */ -  function FormController() { -    var form = this, +  FormController.$inject = ['$scope', '$injector']; +  function FormController($scope, $injector) { +    this.$injector = $injector; + +    var form = this.form = $scope,          $error = form.$error = {};      form.$on('$destroy', function(event){ @@ -257,6 +261,7 @@ function $FormFactoryProvider() {      });      propertiesUpdate(form); +    form.$createWidget = bind(this, this.$createWidget);      function removeWidget(queue, errorKey, widget) {        if (queue) { @@ -354,17 +359,19 @@ function $FormFactoryProvider() {     * @returns {Widget} Instance of a widget scope.     */    FormController.prototype.$createWidget = function(params) { -    var form = this, +    var form = this.form,          modelScope = params.scope,          onChange = params.onChange,          alias = params.alias,          scopeGet = $parse(params.model),          scopeSet = scopeGet.assign, -        widget = this.$new(params.controller, params.controllerArgs); +        widget = form.$new(); + +    this.$injector.instantiate(params.controller, extend({$scope: widget}, params.controllerArgs));      if (!scopeSet) {        throw Error("Expression '" + params.model + "' is not assignable!"); -    }; +    }      widget.$error = {};      // Set the state to something we know will change to get the process going. diff --git a/src/service/http.js b/src/service/http.js index b008aa8e..9d57ed76 100644 --- a/src/service/http.js +++ b/src/service/http.js @@ -395,29 +395,28 @@ function $HttpProvider() {          <doc:example>            <doc:source jsfiddle="false">              <script> -              function FetchCtrl($http) { -                var self = this; -                this.method = 'GET'; -                this.url = 'examples/http-hello.html'; +              function FetchCtrl($scope, $http) { +                $scope.method = 'GET'; +                $scope.url = 'examples/http-hello.html'; -                this.fetch = function() { -                  self.code = null; -                  self.response = null; +                $scope.fetch = function() { +                  $scope.code = null; +                  $scope.response = null; -                  $http({method: self.method, url: self.url}). +                  $http({method: $scope.method, url: $scope.url}).                      success(function(data, status) { -                      self.status = status; -                      self.data = data; +                      $scope.status = status; +                      $scope.data = data;                      }).                      error(function(data, status) { -                      self.data = data || "Request failed"; -                      self.status = status; +                      $scope.data = data || "Request failed"; +                      $scope.status = status;                    });                  }; -                this.updateModel = function(method, url) { -                  self.method = method; -                  self.url = url; +                $scope.updateModel = function(method, url) { +                  $scope.method = method; +                  $scope.url = url;                  };                }              </script> diff --git a/src/service/route.js b/src/service/route.js index 77d94e9c..04bcfdb6 100644 --- a/src/service/route.js +++ b/src/service/route.js @@ -63,8 +63,8 @@      </doc:example>   */  function $RouteProvider(){ -  this.$get = ['$rootScope', '$location', '$routeParams', -      function( $rootScope,  $location,  $routeParams) { +  this.$get = ['$rootScope', '$location', '$routeParams', '$injector', +      function( $rootScope,  $location,  $routeParams, $injector) {      /**       * @ngdoc event       * @name angular.module.ng.$route#$beforeRouteChange @@ -278,8 +278,10 @@ function $RouteProvider(){              }            } else {              copy(next.params, $routeParams); -            (Controller = next.controller) && inferInjectionArgs(Controller); -            next.scope = parentScope.$new(Controller); +            next.scope = parentScope.$new(); +            if (next.controller) { +              $injector.instantiate(next.controller, {$scope: next.scope}); +            }            }          }          $rootScope.$broadcast('$afterRouteChange', next, last); diff --git a/src/service/scope.js b/src/service/scope.js index fe72c953..089e4a41 100644 --- a/src/service/scope.js +++ b/src/service/scope.js @@ -126,8 +126,9 @@ function $RootScopeProvider(){         * @function         *         * @description -       * Creates a new child {@link angular.module.ng.$rootScope.Scope scope}. The new scope can optionally behave as a -       * controller. The parent scope will propagate the {@link angular.module.ng.$rootScope.Scope#$digest $digest()} and +       * Creates a new child {@link angular.module.ng.$rootScope.Scope scope}. +       * +       * The parent scope will propagate the {@link angular.module.ng.$rootScope.Scope#$digest $digest()} and         * {@link angular.module.ng.$rootScope.Scope#$digest $digest()} events. The scope can be removed from the scope         * hierarchy using {@link angular.module.ng.$rootScope.Scope#$destroy $destroy()}.         * @@ -135,13 +136,10 @@ function $RootScopeProvider(){         * the scope and its child scopes to be permanently detached from the parent and thus stop         * participating in model change detection and listener notification by invoking.         * -       * @param {function()=} Class Constructor function which the scope should be applied to the scope. -       * @param {...*} curryArguments Any additional arguments which are curried into the constructor. -       *        See {@link guide/dev_guide.di dependency injection}.         * @returns {Object} The newly created child scope.         *         */ -      $new: function(Class, curryArguments) { +      $new: function() {          var Child = function() {}; // should be anonymous; This is so that when the minifier munges            // the name it does not become random set of chars. These will then show up as class            // name in the debugger. @@ -161,15 +159,6 @@ function $RootScopeProvider(){          } else {            this.$$childHead = this.$$childTail = child;          } -        // short circuit if we have no class -        if (Class) { -          // can't use forEach, we need speed! -          var ClassPrototype = Class.prototype; -          for(var key in ClassPrototype) { -            child[key] = bind(child, ClassPrototype[key]); -          } -          $injector.invoke(Class, child, curryArguments); -        }          return child;        }, diff --git a/src/widget/form.js b/src/widget/form.js index 49e3a545..f3134db4 100644 --- a/src/widget/form.js +++ b/src/widget/form.js @@ -52,8 +52,8 @@      <doc:example>        <doc:source>         <script> -         function Ctrl() { -           this.text = 'guest'; +         function Ctrl($scope) { +           $scope.text = 'guest';           }         </script>         <div ng:controller="Ctrl"> diff --git a/src/widget/input.js b/src/widget/input.js index a744e567..5db52704 100644 --- a/src/widget/input.js +++ b/src/widget/input.js @@ -31,9 +31,9 @@ var INTEGER_REGEXP = /^\s*(\-|\+)?\d+\s*$/;      <doc:example>        <doc:source>         <script> -         function Ctrl() { -           this.text = 'guest'; -           this.word = /^\w*$/; +         function Ctrl($scope) { +           $scope.text = 'guest'; +           $scope.word = /^\w*$/;           }         </script>         <div ng:controller="Ctrl"> @@ -96,8 +96,8 @@ var INTEGER_REGEXP = /^\s*(\-|\+)?\d+\s*$/;      <doc:example>        <doc:source>         <script> -         function Ctrl() { -           this.text = 'me@example.com'; +         function Ctrl($scope) { +           $scope.text = 'me@example.com';           }         </script>         <div ng:controller="Ctrl"> @@ -136,9 +136,8 @@ var INTEGER_REGEXP = /^\s*(\-|\+)?\d+\s*$/;        </doc:scenario>      </doc:example>   */ -angularInputType('email', function() { -  var widget = this; -  this.$on('$validate', function(event){ +angularInputType('email', function(element, widget) { +  widget.$on('$validate', function(event) {      var value = widget.$viewValue;      widget.$emit(!value || value.match(EMAIL_REGEXP) ? "$valid" : "$invalid", "EMAIL");    }); @@ -170,8 +169,8 @@ angularInputType('email', function() {      <doc:example>        <doc:source>         <script> -         function Ctrl() { -           this.text = 'http://google.com'; +         function Ctrl($scope) { +           $scope.text = 'http://google.com';           }         </script>         <div ng:controller="Ctrl"> @@ -210,9 +209,8 @@ angularInputType('email', function() {        </doc:scenario>      </doc:example>   */ -angularInputType('url', function() { -  var widget = this; -  this.$on('$validate', function(event){ +angularInputType('url', function(element, widget) { +  widget.$on('$validate', function(event) {      var value = widget.$viewValue;      widget.$emit(!value || value.match(URL_REGEXP) ? "$valid" : "$invalid", "URL");    }); @@ -239,8 +237,8 @@ angularInputType('url', function() {      <doc:example>        <doc:source>         <script> -         function Ctrl() { -           this.names = ['igor', 'misko', 'vojta']; +         function Ctrl($scope) { +           $scope.names = ['igor', 'misko', 'vojta'];           }         </script>         <div ng:controller="Ctrl"> @@ -270,7 +268,7 @@ angularInputType('url', function() {        </doc:scenario>      </doc:example>   */ -angularInputType('list', function() { +angularInputType('list', function(element, widget) {    function parse(viewValue) {      var list = [];      forEach(viewValue.split(/\s*,\s*/), function(value){ @@ -278,14 +276,14 @@ angularInputType('list', function() {      });      return list;    } -  this.$parseView = function() { -    isString(this.$viewValue) && (this.$modelValue = parse(this.$viewValue)); +  widget.$parseView = function() { +    isString(widget.$viewValue) && (widget.$modelValue = parse(widget.$viewValue));    }; -  this.$parseModel = function() { -    var modelValue = this.$modelValue; +  widget.$parseModel = function() { +    var modelValue = widget.$modelValue;      if (isArray(modelValue) -        && (!isString(this.$viewValue) || !equals(parse(this.$viewValue), modelValue))) { -      this.$viewValue =  modelValue.join(', '); +        && (!isString(widget.$viewValue) || !equals(parse(widget.$viewValue), modelValue))) { +      widget.$viewValue =  modelValue.join(', ');      }    };  }); @@ -318,8 +316,8 @@ angularInputType('list', function() {      <doc:example>        <doc:source>         <script> -         function Ctrl() { -           this.value = 12; +         function Ctrl($scope) { +           $scope.value = 12;           }         </script>         <div ng:controller="Ctrl"> @@ -388,8 +386,8 @@ angularInputType('number', numericRegexpInputType(NUMBER_REGEXP, 'NUMBER'));      <doc:example>        <doc:source>         <script> -         function Ctrl() { -           this.value = 12; +         function Ctrl($scope) { +           $scope.value = 12;           }         </script>         <div ng:controller="Ctrl"> @@ -449,9 +447,9 @@ angularInputType('integer', numericRegexpInputType(INTEGER_REGEXP, 'INTEGER'));      <doc:example>        <doc:source>         <script> -         function Ctrl() { -           this.value1 = true; -           this.value2 = 'YES' +         function Ctrl($scope) { +           $scope.value1 = true; +           $scope.value2 = 'YES'           }         </script>         <div ng:controller="Ctrl"> @@ -477,9 +475,8 @@ angularInputType('integer', numericRegexpInputType(INTEGER_REGEXP, 'INTEGER'));        </doc:scenario>      </doc:example>   */ -angularInputType('checkbox', function(inputElement) { -  var widget = this, -      trueValue = inputElement.attr('ng:true-value'), +angularInputType('checkbox', function(inputElement, widget) { +  var trueValue = inputElement.attr('ng:true-value'),        falseValue = inputElement.attr('ng:false-value');    if (!isString(trueValue)) trueValue = true; @@ -496,7 +493,7 @@ angularInputType('checkbox', function(inputElement) {    };    widget.$parseModel = function() { -    widget.$viewValue = this.$modelValue === trueValue; +    widget.$viewValue = widget.$modelValue === trueValue;    };    widget.$parseView = function() { @@ -522,8 +519,8 @@ angularInputType('checkbox', function(inputElement) {      <doc:example>        <doc:source>         <script> -         function Ctrl() { -           this.color = 'blue'; +         function Ctrl($scope) { +           $scope.color = 'blue';           }         </script>         <div ng:controller="Ctrl"> @@ -545,9 +542,7 @@ angularInputType('checkbox', function(inputElement) {        </doc:scenario>      </doc:example>   */ -angularInputType('radio', function(inputElement) { -  var widget = this; - +angularInputType('radio', function(inputElement, widget) {    //correct the name    inputElement.attr('name', widget.$id + '@' + inputElement.attr('name'));    inputElement.bind('click', function() { @@ -569,9 +564,8 @@ angularInputType('radio', function(inputElement) {  function numericRegexpInputType(regexp, error) { -  return ['$element', function(inputElement) { -    var widget = this, -        min = 1 * (inputElement.attr('min') || Number.MIN_VALUE), +  return function(inputElement, widget) { +    var min = 1 * (inputElement.attr('min') || Number.MIN_VALUE),          max = 1 * (inputElement.attr('max') || Number.MAX_VALUE);      widget.$on('$validate', function(event){ @@ -598,7 +592,7 @@ function numericRegexpInputType(regexp, error) {          ? '' + widget.$modelValue          : '';      }; -  }]; +  };  } @@ -640,8 +634,8 @@ var HTML5_INPUTS_TYPES =  makeMap(      <doc:example>        <doc:source>         <script> -         function Ctrl() { -           this.user = {name: 'guest', last: 'visitor'}; +         function Ctrl($scope) { +           $scope.user = {name: 'guest', last: 'visitor'};           }         </script>         <div ng:controller="Ctrl"> @@ -713,7 +707,8 @@ angularWidget('input', function(inputElement){    this.descend(true);    var modelExp = inputElement.attr('ng:model');    return modelExp && -    ['$defer', '$formFactory', '$element', function($defer, $formFactory, inputElement){ +    ['$defer', '$formFactory', '$element', +        function($defer, $formFactory, inputElement) {        var form = $formFactory.forElement(inputElement),            // We have to use .getAttribute, since jQuery tries to be smart and use the            // type property. Trouble is some browser change unknown to text. @@ -762,7 +757,7 @@ angularWidget('input', function(inputElement){        }        //TODO(misko): setting $inject is a hack -      !TypeController.$inject && (TypeController.$inject = ['$element']); +      !TypeController.$inject && (TypeController.$inject = ['$element', '$scope']);        widget = form.$createWidget({            scope: modelScope,            model: modelExp, @@ -866,7 +861,7 @@ angularWidget('textarea', angularWidget('input'));  function watchElementProperty(modelScope, widget, name, element) {    var bindAttr = fromJson(element.attr('ng:bind-attr') || '{}'), -      match = /\s*{{(.*)}}\s*/.exec(bindAttr[name]), +      match = /\s*\{\{(.*)\}\}\s*/.exec(bindAttr[name]),        isBoolean = BOOLEAN_ATTR[name];    widget['$' + name] = isBoolean      ? ( // some browsers return true some '' when required is set without value. diff --git a/src/widget/select.js b/src/widget/select.js index d4be91d9..b0f5eac5 100644 --- a/src/widget/select.js +++ b/src/widget/select.js @@ -65,15 +65,15 @@      <doc:example>        <doc:source>          <script> -        function MyCntrl() { -          this.colors = [ +        function MyCntrl($scope) { +          $scope.colors = [              {name:'black', shade:'dark'},              {name:'white', shade:'light'},              {name:'red', shade:'dark'},              {name:'blue', shade:'dark'},              {name:'yellow', shade:'light'}            ]; -          this.color = this.colors[2]; // red +          $scope.color = $scope.colors[2]; // red          }          </script>          <div ng:controller="MyCntrl"> @@ -140,11 +140,11 @@ angularWidget('select', function(element){          optionsExp = selectElement.attr('ng:options'),          modelExp = selectElement.attr('ng:model'),          widget = form.$createWidget({ -          scope: this, +          scope: modelScope,            model: modelExp,            onChange: selectElement.attr('ng:change'),            alias: selectElement.attr('name'), -          controller: optionsExp ? Options : (multiple ? Multiple : Single)}); +          controller: ['$scope', optionsExp ? Options : (multiple ? Multiple : Single)]});      selectElement.bind('$destroy', function() { widget.$destroy(); }); @@ -174,11 +174,9 @@ angularWidget('select', function(element){      //////////////////////////// -    function Multiple() { -      var widget = this; - -      this.$render = function() { -        var items = new HashMap(this.$viewValue); +    function Multiple(widget) { +      widget.$render = function() { +        var items = new HashMap(widget.$viewValue);          forEach(selectElement.children(), function(option){            option.selected = isDefined(items.get(option.value));          }); @@ -198,9 +196,7 @@ angularWidget('select', function(element){      } -    function Single() { -      var widget = this; - +    function Single(widget) {        widget.$render = function() {          selectElement.val(widget.$viewValue);        }; @@ -214,9 +210,8 @@ angularWidget('select', function(element){        widget.$viewValue = selectElement.val();      } -    function Options() { -      var widget = this, -          match; +    function Options(widget) { +      var match;        if (! (match = optionsExp.match(NG_OPTIONS_REGEXP))) {          throw Error( @@ -224,8 +219,7 @@ angularWidget('select', function(element){            " but got '" + optionsExp + "'.");        } -      var widgetScope = this, -          displayFn = $parse(match[2] || match[1]), +      var displayFn = $parse(match[2] || match[1]),            valueName = match[4] || match[6],            keyName = match[5],            groupByFn = $parse(match[3] || ''), @@ -253,7 +247,7 @@ angularWidget('select', function(element){        selectElement.html(''); // clear contents        selectElement.bind('change', function() { -        widgetScope.$apply(function() { +        widget.$apply(function() {            var optionGroup,                collection = valuesFn(modelScope) || [],                key = selectElement.val(), @@ -288,13 +282,13 @@ angularWidget('select', function(element){              }            }            if (isDefined(value) && modelScope.$viewVal !== value) { -            widgetScope.$emit('$viewChange', value); +            widget.$emit('$viewChange', value);            }          });        }); -      widgetScope.$watch(render); -      widgetScope.$render = render; +      widget.$watch(render); +      widget.$render = render;        function render() {          var optionGroups = {'':[]}, // Temporary location for the option groups before we render them diff --git a/src/widgets.js b/src/widgets.js index 09a800de..6b3e93ee 100644 --- a/src/widgets.js +++ b/src/widgets.js @@ -54,11 +54,11 @@      <doc:example>        <doc:source jsfiddle="false">         <script> -         function Ctrl() { -           this.templates = +         function Ctrl($scope) { +           $scope.templates =               [ { name: 'template1.html', url: 'examples/ng-include/template1.html'}               , { name: 'template2.html', url: 'examples/ng-include/template2.html'} ]; -           this.template = this.templates[0]; +           $scope.template = $scope.templates[0];           }         </script>         <div ng:controller="Ctrl"> @@ -171,9 +171,9 @@ angularWidget('ng:include', function(element){      <doc:example>        <doc:source>          <script> -          function Ctrl() { -            this.items = ['settings', 'home', 'other']; -            this.selection = this.items[0]; +          function Ctrl($scope) { +            $scope.items = ['settings', 'home', 'other']; +            $scope.selection = $scope.items[0];            }          </script>          <div ng:controller="Ctrl"> @@ -701,10 +701,10 @@ angularWidget('ng:view', function(element) {      <doc:example>        <doc:source>          <script> -          function Ctrl() { -            this.person1 = 'Igor'; -            this.person2 = 'Misko'; -            this.personCount = 1; +          function Ctrl($scope) { +            $scope.person1 = 'Igor'; +            $scope.person2 = 'Misko'; +            $scope.personCount = 1;            }          </script>          <div ng:controller="Ctrl"> | 
