mirror of
https://github.com/Hopiu/angular.js.git
synced 2026-03-17 07:40:22 +00:00
feat($interpolate): string interpolation function
This commit is contained in:
parent
3d0ce0ebe9
commit
0e1fa2aefe
6 changed files with 105 additions and 67 deletions
1
angularFiles.js
vendored
1
angularFiles.js
vendored
|
|
@ -24,6 +24,7 @@ angularFiles = {
|
|||
'src/service/filter/limitTo.js',
|
||||
'src/service/filter/orderBy.js',
|
||||
'src/service/formFactory.js',
|
||||
'src/service/interpolate.js',
|
||||
'src/service/location.js',
|
||||
'src/service/log.js',
|
||||
'src/service/resource.js',
|
||||
|
|
|
|||
|
|
@ -76,6 +76,7 @@ function ngModule($provide, $injector) {
|
|||
$provide.service('$document', $DocumentProvider);
|
||||
$provide.service('$exceptionHandler', $ExceptionHandlerProvider);
|
||||
$provide.service('$filter', $FilterProvider);
|
||||
$provide.service('$interpolate', $InterpolateProvider);
|
||||
$provide.service('$formFactory', $FormFactoryProvider);
|
||||
$provide.service('$http', $HttpProvider);
|
||||
$provide.service('$httpBackend', $HttpBackendProvider);
|
||||
|
|
|
|||
|
|
@ -282,45 +282,6 @@ angularDirective("ng:bind", function(expression, element){
|
|||
}];
|
||||
});
|
||||
|
||||
var bindTemplateCache = {};
|
||||
function compileBindTemplate(template){
|
||||
var fn = bindTemplateCache[template];
|
||||
if (!fn) {
|
||||
var bindings = [];
|
||||
forEach(parseBindings(template), function(text){
|
||||
var exp = binding(text);
|
||||
bindings.push(exp
|
||||
? function(scope, element) { return scope.$eval(exp); }
|
||||
: function() { return text; });
|
||||
});
|
||||
bindTemplateCache[template] = fn = function(scope, element, prettyPrintJson) {
|
||||
var parts = [],
|
||||
hadOwnElement = scope.hasOwnProperty('$element'),
|
||||
oldElement = scope.$element;
|
||||
|
||||
// TODO(misko): get rid of $element
|
||||
scope.$element = element;
|
||||
try {
|
||||
for (var i = 0; i < bindings.length; i++) {
|
||||
var value = bindings[i](scope, element);
|
||||
if (isElement(value))
|
||||
value = '';
|
||||
else if (isObject(value))
|
||||
value = toJson(value, prettyPrintJson);
|
||||
parts.push(value);
|
||||
}
|
||||
return parts.join('');
|
||||
} finally {
|
||||
if (hadOwnElement) {
|
||||
scope.$element = oldElement;
|
||||
} else {
|
||||
delete scope.$element;
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
return fn;
|
||||
}
|
||||
|
||||
/**
|
||||
* @ngdoc directive
|
||||
|
|
|
|||
|
|
@ -51,34 +51,6 @@
|
|||
* Understanding Angular Markup} in the Angular Developer Guide.
|
||||
*/
|
||||
|
||||
function parseBindings(string) {
|
||||
var results = [];
|
||||
var lastIndex = 0;
|
||||
var index;
|
||||
while((index = string.indexOf('{{', lastIndex)) > -1) {
|
||||
if (lastIndex < index)
|
||||
results.push(string.substr(lastIndex, index - lastIndex));
|
||||
lastIndex = index;
|
||||
|
||||
index = string.indexOf('}}', index);
|
||||
index = index < 0 ? string.length : index + 2;
|
||||
|
||||
results.push(string.substr(lastIndex, index - lastIndex));
|
||||
lastIndex = index;
|
||||
}
|
||||
if (lastIndex != string.length)
|
||||
results.push(string.substr(lastIndex, string.length - lastIndex));
|
||||
return results.length === 0 ? [ string ] : results;
|
||||
}
|
||||
|
||||
function binding(string) {
|
||||
var binding = string.replace(/\n/gm, ' ').match(/^\{\{(.*)\}\}$/);
|
||||
return binding ? binding[1] : null;
|
||||
}
|
||||
|
||||
function hasBindings(bindings) {
|
||||
return bindings.length > 1 || binding(bindings[0]) !== null;
|
||||
}
|
||||
|
||||
angularTextMarkup('{{}}', function(text, textNode, parentElement) {
|
||||
var bindings = parseBindings(text),
|
||||
|
|
|
|||
82
src/service/interpolate.js
Normal file
82
src/service/interpolate.js
Normal file
|
|
@ -0,0 +1,82 @@
|
|||
'use strict';
|
||||
|
||||
function $InterpolateProvider(){
|
||||
this.$get = ['$parse', function($parse){
|
||||
return function(text, templateOnly) {
|
||||
var bindings = parseBindings(text);
|
||||
if (hasBindings(bindings) || !templateOnly) {
|
||||
return compileBindTemplate(text);
|
||||
}
|
||||
};
|
||||
}];
|
||||
}
|
||||
|
||||
var bindTemplateCache = {};
|
||||
function compileBindTemplate(template){
|
||||
var fn = bindTemplateCache[template];
|
||||
if (!fn) {
|
||||
var bindings = [];
|
||||
forEach(parseBindings(template), function(text){
|
||||
var exp = binding(text);
|
||||
bindings.push(exp
|
||||
? function(scope, element) { return scope.$eval(exp); }
|
||||
: function() { return text; });
|
||||
});
|
||||
bindTemplateCache[template] = fn = function(scope, element, prettyPrintJson) {
|
||||
var parts = [],
|
||||
hadOwnElement = scope.hasOwnProperty('$element'),
|
||||
oldElement = scope.$element;
|
||||
|
||||
// TODO(misko): get rid of $element
|
||||
scope.$element = element;
|
||||
try {
|
||||
for (var i = 0; i < bindings.length; i++) {
|
||||
var value = bindings[i](scope, element);
|
||||
if (isElement(value))
|
||||
value = '';
|
||||
else if (isObject(value))
|
||||
value = toJson(value, prettyPrintJson);
|
||||
parts.push(value);
|
||||
}
|
||||
return parts.join('');
|
||||
} finally {
|
||||
if (hadOwnElement) {
|
||||
scope.$element = oldElement;
|
||||
} else {
|
||||
delete scope.$element;
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
return fn;
|
||||
}
|
||||
|
||||
|
||||
function parseBindings(string) {
|
||||
var results = [];
|
||||
var lastIndex = 0;
|
||||
var index;
|
||||
while((index = string.indexOf('{{', lastIndex)) > -1) {
|
||||
if (lastIndex < index)
|
||||
results.push(string.substr(lastIndex, index - lastIndex));
|
||||
lastIndex = index;
|
||||
|
||||
index = string.indexOf('}}', index);
|
||||
index = index < 0 ? string.length : index + 2;
|
||||
|
||||
results.push(string.substr(lastIndex, index - lastIndex));
|
||||
lastIndex = index;
|
||||
}
|
||||
if (lastIndex != string.length)
|
||||
results.push(string.substr(lastIndex, string.length - lastIndex));
|
||||
return results.length === 0 ? [ string ] : results;
|
||||
}
|
||||
|
||||
function binding(string) {
|
||||
var binding = string.replace(/\n/gm, ' ').match(/^\{\{(.*)\}\}$/);
|
||||
return binding ? binding[1] : null;
|
||||
}
|
||||
|
||||
function hasBindings(bindings) {
|
||||
return bindings.length > 1 || binding(bindings[0]) !== null;
|
||||
}
|
||||
21
test/service/interpolateSpec.js
Normal file
21
test/service/interpolateSpec.js
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
'use strict';
|
||||
|
||||
describe('$interpolate', function() {
|
||||
|
||||
it('should return a function when there are no bindings and textOnly is undefined',
|
||||
inject(function($interpolate) {
|
||||
expect(typeof $interpolate('some text')).toBe('function');
|
||||
}));
|
||||
|
||||
|
||||
it('should return undefined when there are no bindings and textOnly is set to true',
|
||||
inject(function($interpolate) {
|
||||
expect($interpolate('some text', true)).toBeUndefined();
|
||||
}));
|
||||
|
||||
|
||||
it('should return interpolation function', inject(function($interpolate, $rootScope) {
|
||||
$rootScope.name = 'Misko';
|
||||
expect($interpolate('Hello {{name}}!')($rootScope)).toEqual('Hello Misko!');
|
||||
}));
|
||||
});
|
||||
Loading…
Reference in a new issue