* @ngdoc function * @name ng.$interpolate * @function * * @requires $parse * * @description * * Compiles a string with markup into an interpolation function. This service is used by the * HTML ng.$compile $compile service for data binding. See
(text, mustHaveExpression)
| 4938 | * |
| 4939 | */ |
| 4940 | function $interpolate(text, mustHaveExpression) { |
| 4941 | var startIndex, |
| 4942 | endIndex, |
| 4943 | index = 0, |
| 4944 | parts = [], |
| 4945 | length = text.length, |
| 4946 | hasInterpolation = false, |
| 4947 | fn, |
| 4948 | exp, |
| 4949 | concat = []; |
| 4950 | |
| 4951 | while(index < length) { |
| 4952 | if ( ((startIndex = text.indexOf(startSymbol, index)) != -1) && |
| 4953 | ((endIndex = text.indexOf(endSymbol, startIndex + startSymbolLength)) != -1) ) { |
| 4954 | (index != startIndex) && parts.push(text.substring(index, startIndex)); |
| 4955 | parts.push(fn = $parse(exp = text.substring(startIndex + startSymbolLength, endIndex))); |
| 4956 | fn.exp = exp; |
| 4957 | index = endIndex + endSymbolLength; |
| 4958 | hasInterpolation = true; |
| 4959 | } else { |
| 4960 | // we did not find anything, so we have to add the remainder to the parts array |
| 4961 | (index != length) && parts.push(text.substring(index)); |
| 4962 | index = length; |
| 4963 | } |
| 4964 | } |
| 4965 | |
| 4966 | if (!(length = parts.length)) { |
| 4967 | // we added, nothing, must have been an empty string. |
| 4968 | parts.push(''); |
| 4969 | length = 1; |
| 4970 | } |
| 4971 | |
| 4972 | if (!mustHaveExpression || hasInterpolation) { |
| 4973 | concat.length = length; |
| 4974 | fn = function(context) { |
| 4975 | for(var i = 0, ii = length, part; i<ii; i++) { |
| 4976 | if (typeof (part = parts[i]) == 'function') { |
| 4977 | part = part(context); |
| 4978 | if (part == null || part == undefined) { |
| 4979 | part = ''; |
| 4980 | } else if (typeof part != 'string') { |
| 4981 | part = toJson(part); |
| 4982 | } |
| 4983 | } |
| 4984 | concat[i] = part; |
| 4985 | } |
| 4986 | return concat.join(''); |
| 4987 | }; |
| 4988 | fn.exp = text; |
| 4989 | fn.parts = parts; |
| 4990 | return fn; |
| 4991 | } |
| 4992 | } |
| 4993 | |
| 4994 | |
| 4995 | /** |
no test coverage detected