任何想法如何从指令内部访问属性值?
angular.module('portal.directives',[]) .directive('languageFlag',['$routeParams',function(params) { return function(scope,element,attrs) { console.log(attrs["data-key"]); // returns undefined console.log(element.attr('data-key')); // returns {{data-key}} angular.element(element).css('border','1px solid red'); }; }]);
HTML代码是:
<ul> <li ng-repeat="lng in flags"> <a class="lngFlag {{flag.Key}}" data-key="{{flag.Key}}" data-id="{{lng.Id}}" ng-click="changeLangHash({'lng':lng.Id })" language-flag></a> </li> </ul>
谢谢
使用$观察:
Observing interpolated attributes: Use
$observe
to observe the value changes of attributes that contain interpolation (e.g.src="{{bar}}"
). Not only is this very efficient but it’s also the only way to easily get the actual value because during the linking phase the interpolation hasn’t been evaluated yet and so the value is at this time set toundefined
. — 07000
return function(scope,attrs) { attrs.$observe('key',function(value) { console.log('key=',value); }); }
原文地址:https://www.jb51.cc/angularjs/144097.html
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。