Janvdt
Janvdt

Reputation: 41

Get href value Angularjs

My HTML

<a href="##product.like_url##" df-like-product class="icon-like trigger animated v-a-m" data-id="##product.id##" data-type="product" ng-class="{active:product.liked_by_user}"></a>

I wan't to get the href value in my directive but everytime i get the variable name

I get returned ##product.like_url##

when i inspect the element the url is correct filled in.

This is my directive

df.directives.directive('dfLikeProduct', function($window) {
  return {
    restrict: 'A',
    link: function(scope, element, attrs) {
      // Props
      var $element = $(element);
      var $amount = $element.prev();
      var url = $element.attr('href');

      // Methods
      var likeProduct = function(){
        $.post(url, function(data){ 
          if (data == 1) {
            element.removeClass('unlike').addClass('active like');
            var num = parseFloat($amount.text());
            $amount.text(++num);
          } else if (data == -1) {
            element.removeClass('active like').addClass('unlike');
            var num = parseFloat($amount.text());
            $amount.text(--num);
          }
        }).fail(function(){
          $window.location.href = '/login';
        });
      }


      // Listeners
      $element.on('click', function(e){
        e.preventDefault();
        console.log(url);
        //likeProduct();
      });


      // Init

    }
  };
});

Upvotes: 2

Views: 5027

Answers (1)

P.S.
P.S.

Reputation: 16402

At first you need to use ng-href instead of just href.

https://docs.angularjs.org/api/ng/directive/ngHref

And you can get this using $link.attr('ng-href');

Here is another one useful link: https://docs.angularjs.org/api/ng/function/angular.element

Upvotes: 1

Related Questions