I am learning angular, and I am trying to reduce some code that it takes to
do some common things, like display error messages, by using angular
directives.
One directive I would like to create is like this:
<error-message name="paymentPlanForm.position" error="required">
This field is required.</error-message>
This would generate the following:
<p ng-show="paymentPlanForm.position.$dirty &&
paymentPlanForm.position.$error.required">
<span class="fontawesome-remove"></span> This field is required.</p>
I started to write a directive to accomplish this as follows:
app.directive("errorMessage", function() {
return {
restrict: 'E',
transclude: true,
replace: true,
templateUrl: 'views/partials/errorMessage.html',
link: function(scope, element, attributes) {
scope.name = attributes.name;
scope.error = attributes.error;
}
}});
The template is as follows:
<p ng-show="{{name}}.$dirty && {{name}}.$error.{{error}}">
<span class="fontawesome-remove"></span>
<span ng-transclude></span></p>
I thought this would work, but Angular seems to crash when trying to parse
ng-show inside the template:
Error: [$parse:syntax] Syntax Error: Token '.' not a primary expression at
column 1 of the expression [.$dirty && .$error.] starting at [.$dirty &&
.$error.].
http://errors.angularjs.org/1.2.9/$parse/syntax?p0=.&p1=not%20a%20primary%20expression&p2=1&p3=.%24dirty%20%26%26%20.%24error.&p4=.%24dirty%20%26%26%20.%24error.
minErr/<@http://localhost:8080/keiko/vendor/js/angular.js:78
When I inspect the element in Firebug, the dynamic values have been passed
in successfully, but I guess there's a problem with the scope, or something
else.
How can I get angular to do what I want?
--
You received this message because you are subscribed to the Google Groups
"AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/groups/opt_out.