|
1 | 1 | angular.module('schemaForm').directive('sfMessage',
|
2 |
| -['$injector', 'sfErrorMessage', function($injector, sfErrorMessage) { |
| 2 | +['sfErrorMessage', function(sfErrorMessage) { |
3 | 3 | return {
|
4 | 4 | scope: false,
|
5 | 5 | restrict: 'EA',
|
6 |
| - link: function(scope, element, attrs) { |
7 |
| - |
8 |
| - //Inject sanitizer if it exists |
9 |
| - var $sanitize = $injector.has('$sanitize') ? |
10 |
| - $injector.get('$sanitize') : function(html) { return html; }; |
11 |
| - |
12 |
| - //Prepare and sanitize message, i.e. description in most cases. |
13 |
| - var msg = ''; |
14 |
| - if (attrs.sfMessage) { |
15 |
| - msg = scope.$eval(attrs.sfMessage) || ''; |
16 |
| - msg = $sanitize(msg); |
17 |
| - } |
18 |
| - |
19 |
| - var update = function(valid) { |
20 |
| - if (valid && !scope.hasError()) { |
21 |
| - element.html(msg); |
22 |
| - } else { |
23 |
| - |
24 |
| - var errors = Object.keys( |
25 |
| - (scope.ngModel && scope.ngModel.$error) || {} |
26 |
| - ); |
27 |
| - |
28 |
| - // We only show one error. |
29 |
| - // TODO: Make that optional |
30 |
| - var error = errors[0]; |
31 |
| - |
32 |
| - if (error) { |
33 |
| - element.html(sfErrorMessage.interpolate( |
34 |
| - error, |
35 |
| - scope.ngModel.$modelValue, |
36 |
| - scope.ngModel.$viewValue, |
37 |
| - scope.form, |
38 |
| - scope.options && scope.options.validationMessage |
39 |
| - )); |
40 |
| - } else { |
41 |
| - element.html(msg); |
42 |
| - } |
43 |
| - } |
44 |
| - }; |
45 |
| - update(); |
46 |
| - |
47 |
| - scope.$watchCollection('ngModel.$error', function() { |
48 |
| - if (scope.ngModel) { |
49 |
| - update(scope.ngModel.$valid); |
50 |
| - } |
| 6 | + template: '<div ng-if="!error && form.description" ng-bind-html="form.description"></div><div ng-if="error" ng-bind-html="error"></div>', |
| 7 | + link: function(scope) { |
| 8 | + |
| 9 | + scope.$watchCollection('ngModel.$error', function(errors) { |
| 10 | + |
| 11 | + errors = Object.keys(errors); |
| 12 | + |
| 13 | + // We only show one error. |
| 14 | + // TODO: Make that optional |
| 15 | + var error = errors[0]; |
| 16 | + |
| 17 | + scope.error = error ? |
| 18 | + sfErrorMessage.interpolate( |
| 19 | + error, |
| 20 | + scope.ngModel.$modelValue, |
| 21 | + scope.ngModel.$viewValue, |
| 22 | + scope.form, |
| 23 | + !!(scope.options && scope.options.validationMessage) |
| 24 | + ) |
| 25 | + : null; |
51 | 26 | });
|
52 | 27 |
|
53 | 28 | }
|
|
0 commit comments