From c3a41ff9fefe894663c4d4f40a83794521deb14f Mon Sep 17 00:00:00 2001 From: Misko Hevery Date: Wed, 6 Jun 2012 13:58:10 -0700 Subject: feat($compile): simplify isolate scope bindings Changed the isolate scope binding options to: - @attr - attribute binding (including interpolation) - =model - by-directional model binding - &expr - expression execution binding This change simplifies the terminology as well as number of choices available to the developer. It also supports local name aliasing from the parent. BREAKING CHANGE: isolate scope bindings definition has changed and the inject option for the directive controller injection was removed. To migrate the code follow the example below: Before: scope: { myAttr: 'attribute', myBind: 'bind', myExpression: 'expression', myEval: 'evaluate', myAccessor: 'accessor' } After: scope: { myAttr: '@', myBind: '@', myExpression: '&', // myEval - usually not useful, but in cases where the expression is assignable, you can use '=' myAccessor: '=' // in directive's template change myAccessor() to myAccessor } The removed `inject` wasn't generaly useful for directives so there should be no code using it. --- src/ng/directive/input.js | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) (limited to 'src/ng/directive/input.js') diff --git a/src/ng/directive/input.js b/src/ng/directive/input.js index aa79082b..04af4c2a 100644 --- a/src/ng/directive/input.js +++ b/src/ng/directive/input.js @@ -857,8 +857,8 @@ var VALID_CLASS = 'ng-valid', * * */ -var NgModelController = ['$scope', '$exceptionHandler', '$attrs', 'ngModel', '$element', - function($scope, $exceptionHandler, $attr, ngModel, $element) { +var NgModelController = ['$scope', '$exceptionHandler', '$attrs', '$element', '$parse', + function($scope, $exceptionHandler, $attr, $element, $parse) { this.$viewValue = Number.NaN; this.$modelValue = Number.NaN; this.$parsers = []; @@ -870,6 +870,14 @@ var NgModelController = ['$scope', '$exceptionHandler', '$attrs', 'ngModel', '$e this.$invalid = false; this.$name = $attr.name; + var ngModelGet = $parse($attr.ngModel), + ngModelSet = ngModelGet.assign; + + if (!ngModelSet) { + throw Error(NON_ASSIGNABLE_MODEL_EXPRESSION + $attr.ngModel + + ' (' + startingTag($element) + ')'); + } + /** * @ngdoc function * @name angular.module.ng.$compileProvider.directive.ngModel.NgModelController#$render @@ -974,7 +982,7 @@ var NgModelController = ['$scope', '$exceptionHandler', '$attrs', 'ngModel', '$e if (this.$modelValue !== value) { this.$modelValue = value; - ngModel(value); + ngModelSet($scope, value); forEach(this.$viewChangeListeners, function(listener) { try { listener(); @@ -987,9 +995,7 @@ var NgModelController = ['$scope', '$exceptionHandler', '$attrs', 'ngModel', '$e // model -> value var ctrl = this; - $scope.$watch(function() { - return ngModel(); - }, function(value) { + $scope.$watch(ngModelGet, function(value) { // ignore change from view if (ctrl.$modelValue === value) return; @@ -1044,9 +1050,6 @@ var NgModelController = ['$scope', '$exceptionHandler', '$attrs', 'ngModel', '$e */ var ngModelDirective = function() { return { - inject: { - ngModel: 'accessor' - }, require: ['ngModel', '^?form'], controller: NgModelController, link: function(scope, element, attr, ctrls) { -- cgit v1.2.3