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

Commit 7dc7feb

Browse files
committed
docs(ngProp, ngOn)
1 parent 494277e commit 7dc7feb

File tree

1 file changed

+142
-0
lines changed

1 file changed

+142
-0
lines changed

src/ng/compile.js

+142
Original file line numberDiff line numberDiff line change
@@ -1044,6 +1044,148 @@
10441044
*
10451045
*/
10461046

1047+
/**
1048+
* @ngdoc directive
1049+
* @name ngProp
1050+
* @restrict A
1051+
* @element ANY
1052+
*
1053+
* @description
1054+
* The `ngProp` directive binds an expression to a DOM element property.
1055+
* `ngProp` allows writing to arbitrary properties by including
1056+
* the property name in the attribute, e.g. `ng-prop-value="'my value'"` binds 'my value' to
1057+
* the `value` property.
1058+
*
1059+
* Usually, it's not necessary to write to properties in AngularJS, as the built-in directives
1060+
* handle the most common use cases (instead of the above example, you would use {@link ngValue}).
1061+
*
1062+
* However, [custom elements](https://developer.mozilla.org/docs/Web/Web_Components/Using_custom_elements)
1063+
* often use custom properties to hold data, and `ngProp` can be used
1064+
* for this purpose.
1065+
*
1066+
* ## Binding to camelCase properties
1067+
*
1068+
* Since HTML attributes are case-insensitive, camelCase properties like `innerHTML` must be escaped.
1069+
* AngularJS uses the underscore (_) in front of a character to indicate that it is uppercase, so
1070+
* `innerHTML` must be written as `ng-prop-inner_h_t_m_l="expression"`.
1071+
*
1072+
* ## Security
1073+
*
1074+
* Binding expressions to arbitrary properties poses a security risk, as properties like `innerHTML`
1075+
* can insert potentially dangerous HTML into the application, e.g. script tags that execute
1076+
* malicious code.
1077+
* For this reason, `ngProp` requires trusted expression content via
1078+
* Strict Contextual Escaping with the {@link ng.$sce $sce service}. Practically this means
1079+
* that different properties require different trusted content types
1080+
*
1081+
* ### Sanitization
1082+
*
1083+
* By default, `$sce` will throw an error if it detects potentially dangerous content. However,
1084+
* if you include the {@link ngSanitize ngSanitize module}, it will try to strip the
1085+
* potentially dangerous content, i.e. script tags in the `innerHTML` property.
1086+
*
1087+
*/
1088+
1089+
/** @ngdoc directive
1090+
* @name ngOn
1091+
* @restrict A
1092+
* @element ANY
1093+
*
1094+
* @description
1095+
* The `ngOn` directive adds an event listener to a DOM element, and evaluates an expression
1096+
* when the event is fired.
1097+
* `ngOn` allows adding listeners for arbitrary events by including
1098+
* the event name in the attribute, e.g. `ng-on-drop="onDrop()"` executes the 'onDrop' expression
1099+
* when the `drop` event is fired.
1100+
*
1101+
* AngularJS provides specific directives for many events, such as {@link ngClick}, so in most
1102+
* cases it is not necessary to use `ngOn`. However, AngularJS does not support all events
1103+
* (e.g. the `drop` event in the example above), and
1104+
* [custom elements](https://developer.mozilla.org/docs/Web/Web_Components/Using_custom_elements)
1105+
* often use custom events that are fired like normal DOM events.
1106+
*
1107+
* @example
1108+
* ### Bind to built-in DOM events
1109+
*
1110+
* <example name="ngOn" module="exampleNgOn">
1111+
* <file name="app.js">
1112+
* angular.module('exampleNgOn', [])
1113+
* .component('main', {
1114+
* templateUrl: 'main.html',
1115+
* controller: function() {
1116+
* this.clickCount = 0;
1117+
* this.mouseoverCount = 0;
1118+
*
1119+
* this.loadingState = 0;
1120+
* }
1121+
* });
1122+
* </file>
1123+
* <file name="main.html">
1124+
* <div>
1125+
* This is equivalent to `ngClick` and `ngMouseover`:<br>
1126+
* <button
1127+
* ng-on-click="$ctrl.clickCount = $ctrl.clickCount + 1"
1128+
* ng-on-mouseover="$ctrl.mouseoverCount = $ctrl.mouseoverCount + 1">Click or mouseover</button><br>
1129+
* clickCount: {{$ctrl.clickCount}}<br>
1130+
* mouseover: {{$ctrl.mouseoverCount}}
1131+
*
1132+
* <hr>
1133+
*
1134+
* For the `error` and `load` event on images no built-in AngularJS directives exist:<br>
1135+
* <img src="thisimagedoesnotexist.png" ng-on-error="$ctrl.loadingState = -1" ng-on-load="$ctrl.loadingState = 1"><br>
1136+
* <div ng-switch="$ctrl.loadingState">
1137+
* <span ng-switch-when="0">Image is loading</span>
1138+
* <span ng-switch-when="-1">Image load error</span>
1139+
* <span ng-switch-when="1">Image loaded successfully</span>
1140+
* </div>
1141+
* </div>
1142+
* </file>
1143+
* <file name="index.html">
1144+
* <main></main>
1145+
* </file>
1146+
* </example>
1147+
*
1148+
*
1149+
* @example
1150+
* ### Bind to custom DOM events
1151+
*
1152+
* <example name="ngOnCustom" module="exampleNgOn">
1153+
* <file name="app.js">
1154+
* angular.module('exampleNgOn', [])
1155+
* .component('main', {
1156+
* templateUrl: 'main.html',
1157+
* controller: function() {
1158+
* this.eventLog = '';
1159+
*
1160+
* this.listener = function($event) {
1161+
* this.eventLog = 'Event with type "' + $event.type + '" fired at ' + $event.detail;
1162+
* };
1163+
* }
1164+
* })
1165+
* .component('childComponent', {
1166+
* templateUrl: 'child.html',
1167+
* controller: function($element) {
1168+
* this.fireEvent = function() {
1169+
* var event = new CustomEvent('customtype', { detail: new Date()});
1170+
*
1171+
* $element[0].dispatchEvent(event);
1172+
* };
1173+
* }
1174+
* });
1175+
* </file>
1176+
* <file name="main.html">
1177+
* <child-component ng-on-customtype="$ctrl.listener($event)"></child-component><br>
1178+
* <span>Event log: {{$ctrl.eventLog}}</span>
1179+
* </file>
1180+
* <file name="child.html">
1181+
<button ng-click="$ctrl.fireEvent()">Fire custom event</button>
1182+
* </file>
1183+
* <file name="index.html">
1184+
* <main></main>
1185+
* </file>
1186+
* </example>
1187+
*/
1188+
10471189
var $compileMinErr = minErr('$compile');
10481190

10491191
function UNINITIALIZED_VALUE() {}

0 commit comments

Comments
 (0)