Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 1c03a1b

Browse files
docs(ngModelController): clarify issue with isolated scope directive
See angular#4043
1 parent fd797cd commit 1c03a1b

File tree

1 file changed

+34
-4
lines changed

1 file changed

+34
-4
lines changed

src/ng/directive/input.js

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -828,11 +828,8 @@ var VALID_CLASS = 'ng-valid',
828828
* purposefully does not contain any logic which deals with DOM rendering or listening to
829829
* DOM events. Such DOM related logic should be provided by other directives which make use of
830830
* `NgModelController` for data-binding.
831-
* Note that you cannot use `NgModelController` in a directive with an isolated scope,
832-
* as, in that case, the `ng-model` value gets put into the isolated scope and does not get
833-
* propagated to the parent scope.
834-
*
835831
*
832+
* ## Custom Control Example
836833
* This example shows how to use `NgModelController` with a custom control to achieve
837834
* data-binding. Notice how different directives (`contenteditable`, `ng-model`, and `required`)
838835
* collaborate together to achieve the desired result.
@@ -910,6 +907,39 @@ var VALID_CLASS = 'ng-valid',
910907
</file>
911908
* </example>
912909
*
910+
* ## Isolated Scope Pitfall
911+
*
912+
* Note that if you have a directive with an isolated scope, you cannot require `ngModel`
913+
* since the model value will be looked up on the isolated scope rather than the outer scope.
914+
* When the directive updates the model value, calling `ngModel.$setViewValue()` the property
915+
* on the outer scope will not be updated.
916+
*
917+
* Here is an example of this situation. You'll notice that even though both 'input' and 'div'
918+
* seem to be attached to the same model, they are not kept in synch.
919+
*
920+
* <example module="badIsolatedDirective">
921+
<file name="script.js">
922+
angular.module('badIsolatedDirective', []).directive('bad', function() {
923+
return {
924+
require: 'ngModel',
925+
scope: { },
926+
template: '<input ng-model="innerModel">',
927+
link: function(scope, element, attrs, ngModel) {
928+
scope.$watch('innerModel', function(value) {
929+
console.log(value);
930+
ngModel.$setViewValue(value);
931+
});
932+
}
933+
};
934+
});
935+
</file>
936+
<file name="index.html">
937+
<input ng-model="someModel">
938+
<div bad ng-model="someModel"></div>
939+
</file>
940+
* </example>
941+
*
942+
*
913943
*/
914944
var NgModelController = ['$scope', '$exceptionHandler', '$attrs', '$element', '$parse',
915945
function($scope, $exceptionHandler, $attr, $element, $parse) {

0 commit comments

Comments
 (0)