javascript - Unable to access AngularJS attribute in directive -
i'm trying implement simple directive in angularjs. in end, want able use directive this:
<share-buttons url="google.com"></share-buttons>
my directive looks (coffee script):
module.directive 'sharebuttons', () -> controller = ['$scope', ($scope) -> $scope.share = () -> console.log $scope.url ] return { restrict: 'ea' scope: { url: '=url' } templateurl: viewpath_common('/directives/share-buttons') controller: controller }
and here's template (jade):
.social-icons button.btn.btn-li(ng-click="share()") i.fa.fa-linkedin.fa-fw
when click button, correct function called ($scope.share), thing logged undefined
.
can me?
its because url
attribute 2 way data bound "="
so it's looking scope variable this:
$scope.google.com = // should value.
to passed controller directive.
if want string passed, use "@" 1 way binding
scope: { url: '@' }
note: can access via attributes in link function of directive (which don't have above)
// restrict: 'ea' scope: { url: '@' } templateurl: "someurl", link: function(scope, elem, attrs) { console.log(attrs.url); }
Comments
Post a Comment