1

I have an angular directive that isn't quite rendering correctly.

here's the html:

<cloud-login ng-repeat='c in ctrl.clouds' cloud="{{c}}"></cloud-login>

and the directive:

function CloudLoginDirective() {
  return {
  restrict: 'AE',
  template: '<a ng-click="ctrl.cloudLogin("{{cloud}}")"><img ng-src="../images/{{cloud}}.png"/></a>',
  link: function(scope, element, attrs) {
    attrs.$observe('cloud', function(cloud) {
      scope.cloud = cloud;
    });
  }
 };
}

In my template, only the ng-src correctly populates with the appropriate text.

The ng-click does not.

Any hints?

Best.

1
  • ng-click="expression" ng-src="string" Commented May 20, 2015 at 2:48

1 Answer 1

4

Your issue is due to the invalid expression caused by the ng-click expression: <a ng-click="ctrl.cloudLogin("{{cloud}}")"> This results in syntax error due to all those mismatching or pre-maturely terminating quotes. ng-click takes an expression you don't have to interpolate it. Just do:

<a ng-click="ctrl.cloudLogin(cloud)">

Plnkr

You could also make your directive much simpler removing all those attribute watches and using isolated scope and function binding and decoupling cloud directive from its parent controller, like this:

.directive('cloudLogin', function CloudLoginDirective() {
  return {
  restrict: 'AE',
  scope:{
    cloud:"=", // Using a 2 way binding, if you need only text then just use @ combined with {{c}} when used
    onClick:"&" //Function binding
  },
  template: '<a ng-click="onClick({cloud: cloud})"><img ng-src="../images/{{cloud}}.png"/>{{cloud}}</a>'
 };
});

and

<cloud-login ng-repeat='c in ctrl.clouds' 
             cloud="c" 
             on-click="ctrl.cloudLogin(cloud)"></cloud-login>

Plnkr

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.