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

Commit d606e66

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

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
@@ -1466,7 +1466,12 @@ var VALID_CLASS = 'ng-valid',
14661466
* Note that `contenteditable` is an HTML5 attribute, which tells the browser to let the element
14671467
* contents be edited in place by the user. This will not work on older browsers.
14681468
*
1469-
* <example name="NgModelController" module="customControl">
1469+
* We are using the {@link ng.service:$sce $sce} service here and include the {@link ngSanitize $sanitize}
1470+
* module to automatically remove "bad" content like inline event listener (e.g. `<span onclick="...">`).
1471+
* However, as we are using `$sce` the model can still decide to to provide unsafe content if it marks
1472+
* that content using the `$sce` service.
1473+
*
1474+
* <example name="NgModelController" module="customControl" deps="angular-sanitize.js">
14701475
<file name="style.css">
14711476
[contenteditable] {
14721477
border: 1px solid black;
@@ -1480,8 +1485,8 @@ var VALID_CLASS = 'ng-valid',
14801485
14811486
</file>
14821487
<file name="script.js">
1483-
angular.module('customControl', []).
1484-
directive('contenteditable', function() {
1488+
angular.module('customControl', ['ngSanitize']).
1489+
directive('contenteditable', ['$sce', function($sce) {
14851490
return {
14861491
restrict: 'A', // only activate on element attribute
14871492
require: '?ngModel', // get a hold of NgModelController
@@ -1490,7 +1495,7 @@ var VALID_CLASS = 'ng-valid',
14901495
14911496
// Specify how UI should be updated
14921497
ngModel.$render = function() {
1493-
element.html(ngModel.$viewValue || '');
1498+
element.html($sce.getTrustedHtml(ngModel.$viewValue || ''));
14941499
};
14951500
14961501
// Listen for change events to enable binding
@@ -1511,7 +1516,7 @@ var VALID_CLASS = 'ng-valid',
15111516
}
15121517
}
15131518
};
1514-
});
1519+
}]);
15151520
</file>
15161521
<file name="index.html">
15171522
<form name="myForm">

0 commit comments

Comments
 (0)