2017-02-10 89 views
0

我想我只是在做一些愚蠢的事情,但我想访问一个HTML元素的自定义属性,我附加了一个指令,但它不起作用。我见过其他人使用范围。$ eval,但我似乎无法得到这个工作。无法从Angularjs指令访问属性

这里就是我想(的jsfiddle这里:https://jsfiddle.net/u5d3gfny/1/):

<!DOCTYPE html> 
<html ng-app="AngAttrTest"> 
    <head> 
     <title>Bootstrap 3</title> 
    </head> 
    <meta name="viewport" content="width=device-width, initial-scale=1.0"/> 


    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous"> 
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.7/angular.min.js"></script> 
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script> 

    <body>  

     <a href="#" id="my-modal" class="btn btn-success fcvt-btn-save" nextModalId="fileFormatModal" chain-modal>Continue</a> 
     <div id="appendMe"/> 

     <script> 

      var app = angular.module('AngAttrTest', []); 

      app.directive('chainModal', ['$document', function($document){ 

       return { 

        link: function(scope, elem, attrs) { 
         elem.bind('click', function() { 
          $('#appendMe').append("This works, ID: " + attrs.id + "<br/><br/>"); 
          $('#appendMe').append("So does this, Class: " + attrs.class + "<br/><br/>"); 
          $('#appendMe').append("But my custom Attribute nextModalId comes up undefined: " + attrs.nextModalId + "<br/><br/>"); 
         }); 
        } 

       } 
      }]);   

     </script> 

    </body> 
</html> 

我也试图与该更换我的链接功能没有avial:

   link: function(scope, elem, attrs) { 
        elem.bind('click', function() { 
          var nextModalId = scope.$eval(attrs.nextModalId); 

         $('#appendMe').append("This works, ID: " + attrs.id + "<br/><br/>"); 
         $('#appendMe').append("So does this, Class: " + attrs.class + "<br/><br/>"); 
         $('#appendMe').append("But my custom Attribute nextModalId comes up undefined: " + nextModalId + "<br/><br/>"); 


        }); 
       } 

这似乎应该是很简单但我无法弄清楚我做错了什么傻事。谢谢!

回答

0

当指令被用作属性时,它必须在snake-case中声明。要解决......

更改nextModalId

<a href="#" 
    id="my-modal" 
    class="btn btn-success fcvt-btn-save" 
    nextModalId="fileFormatModal" 
    chain-modal> 
    Continue 
</a> 

next-modal-id

<a href="#" 
    id="my-modal" 
    class="btn btn-success fcvt-btn-save" 
    next-modal-id="fileFormatModal" 
    chain-modal> 
    Continue 
</a> 

Updated JSFiddle

+0

是的,并再次感谢。我应该知道它必须是蛇的情况下,因为一切都是:)。 –

0

的问题是与elem.bind功能不是指令。我改变了这种方式的代码:

 var app = angular.module('AngAttrTest', []); 

     app.directive('chainModal', ['$document', function($document){ 

      return { 
       link: function(scope, elem, attrs) { 
        elem.bind('click', { id: attrs['id'], class: attrs['class'], nextModalId: attrs['nextModalId'] }, function(event) { 
         $('#appendMe').append("This works, ID: " + event.data.id + "<br/><br/>"); 
         $('#appendMe').append("So does this, Class: " + event.data.class + "<br/><br/>"); 
         $('#appendMe').append("But my custom Attribute nextModalId comes up undefined: " + event.data.nextModalId + "<br/><br/>"); 
        }); 
       } 
      } 
     }]);