0

I have some HTML which looks like:

<custom-logo>
    <div>
        <img src="logo.png">
    </div>
    <custom-logo-after>
    </custom-logo-after>
</custom-logo>

I'm trying to figure out how I can amend the custom-logo directive in Angular JS to include a link around the img tag so I could add a dynamic link address in Angular JS.

3
  • show us the directive please Commented Jun 2, 2016 at 12:34
  • I'm not sure what you mean. Sorry very new to this. Commented Jun 2, 2016 at 12:47
  • 1
    You have a directive called custom-logo - We don't have this directive, so we need to see its code and template in order to help Commented Jun 2, 2016 at 12:48

2 Answers 2

3

You can use ng-transclude to do that

.directive('customLogo', function() {
    return {
        transclude: true,
        template: '<a href="http://www.google.com"><ng-transclude></ng-transclude></a>'
    }
});
Sign up to request clarification or add additional context in comments.

Comments

0

You can do it like this Link to fiddle

angular.module('directives', []).directive('customLogo', 
    function() {
      return {
        restrict: 'E',
        link: function($scope, element, attrs) {
         var image =  element.find('img');
         image.attr("src","blabla.png");
        }
      };
    }
  );
angular.module('myApp', ['directives']);


<div ng-app="myApp">
   <custom-logo>
      <div>
        <img src="logo.png">
      </div>
  </custom-logo>

</div>

note that "blabla.png" can be a reference to you image file on your scope. so for instance $scope.image = "src/images/blabla.png" and then replace

image.attr("src",$scope.image);

1 Comment

Unfortunately I can't edit the existing html but can run custom angular js so looking for a way to amend the existing content

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.