@@ -934,7 +934,12 @@ var VALID_CLASS = 'ng-valid',
934
934
* Note that `contenteditable` is an HTML5 attribute, which tells the browser to let the element
935
935
* contents be edited in place by the user. This will not work on older browsers.
936
936
*
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">
938
943
<file name="style.css">
939
944
[contenteditable] {
940
945
border: 1px solid black;
@@ -948,8 +953,8 @@ var VALID_CLASS = 'ng-valid',
948
953
949
954
</file>
950
955
<file name="script.js">
951
- angular.module('customControl', []).
952
- directive('contenteditable', function() {
956
+ angular.module('customControl', ['ngSanitize' ]).
957
+ directive('contenteditable', ['$sce', function($sce ) {
953
958
return {
954
959
restrict: 'A', // only activate on element attribute
955
960
require: '?ngModel', // get a hold of NgModelController
@@ -958,7 +963,7 @@ var VALID_CLASS = 'ng-valid',
958
963
959
964
// Specify how UI should be updated
960
965
ngModel.$render = function() {
961
- element.html(ngModel.$viewValue || '');
966
+ element.html($sce.getTrustedHtml( ngModel.$viewValue || '') );
962
967
};
963
968
964
969
// Listen for change events to enable binding
@@ -979,7 +984,7 @@ var VALID_CLASS = 'ng-valid',
979
984
}
980
985
}
981
986
};
982
- });
987
+ }] );
983
988
</file>
984
989
<file name="index.html">
985
990
<form name="myForm">
0 commit comments