Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit e9ecd56

Browse files
committed
docs(ngModelController): use $sce and $sanitize in the contenteditable example.
Closes #7464
1 parent f107ef8 commit e9ecd56

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
lines changed

src/ng/directive/input.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -934,7 +934,12 @@ var VALID_CLASS = 'ng-valid',
934934
* Note that `contenteditable` is an HTML5 attribute, which tells the browser to let the element
935935
* contents be edited in place by the user. This will not work on older browsers.
936936
*
937-
* <example name="NgModelController" module="customControl">
937+
* We are using the {@link ng.service:$sce $sce} service here and include the {@link ngSanitize $sanitize}
938+
* module to automatically remove "bad" content like inline event listener (e.g. `<span onclick="...">`).
939+
* However, as we are using `$sce` the model can still decide to to provide unsafe content if it marks
940+
* that content using the `$sce` service.
941+
*
942+
* <example name="NgModelController" module="customControl" deps="angular-sanitize.js">
938943
<file name="style.css">
939944
[contenteditable] {
940945
border: 1px solid black;
@@ -948,8 +953,8 @@ var VALID_CLASS = 'ng-valid',
948953
949954
</file>
950955
<file name="script.js">
951-
angular.module('customControl', []).
952-
directive('contenteditable', function() {
956+
angular.module('customControl', ['ngSanitize']).
957+
directive('contenteditable', ['$sce', function($sce) {
953958
return {
954959
restrict: 'A', // only activate on element attribute
955960
require: '?ngModel', // get a hold of NgModelController
@@ -958,7 +963,7 @@ var VALID_CLASS = 'ng-valid',
958963
959964
// Specify how UI should be updated
960965
ngModel.$render = function() {
961-
element.html(ngModel.$viewValue || '');
966+
element.html($sce.getTrustedHtml(ngModel.$viewValue || ''));
962967
};
963968
964969
// Listen for change events to enable binding
@@ -979,7 +984,7 @@ var VALID_CLASS = 'ng-valid',
979984
}
980985
}
981986
};
982-
});
987+
}]);
983988
</file>
984989
<file name="index.html">
985990
<form name="myForm">

0 commit comments

Comments
 (0)