mirror of
https://github.com/Hopiu/angular.js.git
synced 2026-03-26 19:20:24 +00:00
139 lines
5.1 KiB
JavaScript
139 lines
5.1 KiB
JavaScript
'use strict';
|
|
|
|
/**
|
|
* @ngdoc directive
|
|
* @name angular.module.ng.$compileProvider.directive.ngBind
|
|
*
|
|
* @description
|
|
* The `ngBind` attribute tells Angular to replace the text content of the specified HTML element
|
|
* with the value of a given expression, and to update the text content when the value of that
|
|
* expression changes.
|
|
*
|
|
* Typically, you don't use `ngBind` directly, but instead you use the double curly markup like
|
|
* `{{ expression }}` which is similar but less verbose.
|
|
*
|
|
* Once scenario in which the use of `ngBind` is prefered over `{{ expression }}` binding is when
|
|
* it's desirable to put bindings into template that is momentarily displayed by the browser in its
|
|
* raw state before Angular compiles it. Since `ngBind` is an element attribute, it makes the
|
|
* bindings invisible to the user while the page is loading.
|
|
*
|
|
* An alternative solution to this problem would be using the
|
|
* {@link angular.module.ng.$compileProvider.directive.ngCloak ngCloak} directive.
|
|
*
|
|
*
|
|
* @element ANY
|
|
* @param {expression} ngBind {@link guide/expression Expression} to evaluate.
|
|
*
|
|
* @example
|
|
* Enter a name in the Live Preview text box; the greeting below the text box changes instantly.
|
|
<doc:example>
|
|
<doc:source>
|
|
<script>
|
|
function Ctrl($scope) {
|
|
$scope.name = 'Whirled';
|
|
}
|
|
</script>
|
|
<div ng-controller="Ctrl">
|
|
Enter name: <input type="text" ng-model="name"><br>
|
|
Hello <span ng-bind="name"></span>!
|
|
</div>
|
|
</doc:source>
|
|
<doc:scenario>
|
|
it('should check ng-bind', function() {
|
|
expect(using('.doc-example-live').binding('name')).toBe('Whirled');
|
|
using('.doc-example-live').input('name').enter('world');
|
|
expect(using('.doc-example-live').binding('name')).toBe('world');
|
|
});
|
|
</doc:scenario>
|
|
</doc:example>
|
|
*/
|
|
var ngBindDirective = ngDirective(function(scope, element, attr) {
|
|
element.addClass('ng-binding').data('$binding', attr.ngBind);
|
|
scope.$watch(attr.ngBind, function(value) {
|
|
element.text(value == undefined ? '' : value);
|
|
});
|
|
});
|
|
|
|
|
|
/**
|
|
* @ngdoc directive
|
|
* @name angular.module.ng.$compileProvider.directive.ngBindTemplate
|
|
*
|
|
* @description
|
|
* The `ngBindTemplate` directive specifies that the element
|
|
* text should be replaced with the template in ngBindTemplate.
|
|
* Unlike ngBind the ngBindTemplate can contain multiple `{{` `}}`
|
|
* expressions. (This is required since some HTML elements
|
|
* can not have SPAN elements such as TITLE, or OPTION to name a few.)
|
|
*
|
|
* @element ANY
|
|
* @param {string} ngBindTemplate template of form
|
|
* <tt>{{</tt> <tt>expression</tt> <tt>}}</tt> to eval.
|
|
*
|
|
* @example
|
|
* Try it here: enter text in text box and watch the greeting change.
|
|
<doc:example>
|
|
<doc:source>
|
|
<script>
|
|
function Ctrl($scope) {
|
|
$scope.salutation = 'Hello';
|
|
$scope.name = 'World';
|
|
}
|
|
</script>
|
|
<div ng-controller="Ctrl">
|
|
Salutation: <input type="text" ng-model="salutation"><br>
|
|
Name: <input type="text" ng-model="name"><br>
|
|
<pre ng-bind-template="{{salutation}} {{name}}!"></pre>
|
|
</div>
|
|
</doc:source>
|
|
<doc:scenario>
|
|
it('should check ng-bind', function() {
|
|
expect(using('.doc-example-live').binding('salutation')).
|
|
toBe('Hello');
|
|
expect(using('.doc-example-live').binding('name')).
|
|
toBe('World');
|
|
using('.doc-example-live').input('salutation').enter('Greetings');
|
|
using('.doc-example-live').input('name').enter('user');
|
|
expect(using('.doc-example-live').binding('salutation')).
|
|
toBe('Greetings');
|
|
expect(using('.doc-example-live').binding('name')).
|
|
toBe('user');
|
|
});
|
|
</doc:scenario>
|
|
</doc:example>
|
|
*/
|
|
var ngBindTemplateDirective = ['$interpolate', function($interpolate) {
|
|
return function(scope, element, attr) {
|
|
// TODO: move this to scenario runner
|
|
var interpolateFn = $interpolate(element.attr(attr.$attr.ngBindTemplate));
|
|
element.addClass('ng-binding').data('$binding', interpolateFn);
|
|
attr.$observe('ngBindTemplate', function(value) {
|
|
element.text(value);
|
|
});
|
|
}
|
|
}];
|
|
|
|
|
|
/**
|
|
* @ngdoc directive
|
|
* @name angular.module.ng.$compileProvider.directive.ngBindHtmlUnsafe
|
|
*
|
|
* @description
|
|
* Creates a binding that will innerHTML the result of evaluating the `expression` into the current
|
|
* element. *The innerHTML-ed content will not be sanitized!* You should use this directive only if
|
|
* {@link angular.module.ngSanitize.directive.ngBindHtml ngBindHtml} directive is too
|
|
* restrictive and when you absolutely trust the source of the content you are binding to.
|
|
*
|
|
* See {@link angular.module.ngSanitize.$sanitize $sanitize} docs for examples.
|
|
*
|
|
* @element ANY
|
|
* @param {expression} ngBindHtmlUnsafe {@link guide/expression Expression} to evaluate.
|
|
*/
|
|
var ngBindHtmlUnsafeDirective = [function() {
|
|
return function(scope, element, attr) {
|
|
element.addClass('ng-binding').data('$binding', attr.ngBindHtmlUnsafe);
|
|
scope.$watch(attr.ngBindHtmlUnsafe, function(value) {
|
|
element.html(value || '');
|
|
});
|
|
};
|
|
}];
|