@@ -828,11 +828,8 @@ var VALID_CLASS = 'ng-valid',
828
828
* purposefully does not contain any logic which deals with DOM rendering or listening to
829
829
* DOM events. Such DOM related logic should be provided by other directives which make use of
830
830
* `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
- *
835
831
*
832
+ * ## Custom Control Example
836
833
* This example shows how to use `NgModelController` with a custom control to achieve
837
834
* data-binding. Notice how different directives (`contenteditable`, `ng-model`, and `required`)
838
835
* collaborate together to achieve the desired result.
@@ -910,6 +907,39 @@ var VALID_CLASS = 'ng-valid',
910
907
</file>
911
908
* </example>
912
909
*
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
+ *
913
943
*/
914
944
var NgModelController = [ '$scope' , '$exceptionHandler' , '$attrs' , '$element' , '$parse' ,
915
945
function ( $scope , $exceptionHandler , $attr , $element , $parse ) {
0 commit comments