Saturday 15 September 2012

javascript - get the value of the child element of directive -



javascript - get the value of the child element of directive -

i have these directive

app.directive("myarticle",function($timeout){ homecoming { restrict: "e", replace: true, templateurl: "templates/article.html", link: function(scope, element, attrs) { element.getval() } , scope: { cat: "=", mainurl: "=" } } });

here directive template

<div class="span2 "> <a href="#/cat/{{cat.id}}"> link cat</a> </div>

the jquery getval function

(function($) { $.fn.getval = function() { this.each(function() { var url =$(this).find("a").attr("href"); console.log(url); }); }; })(jquery);

the function long , tried simplify can

what expected render "#/cats/1" rendered "#cats/{{cat.id}}"

so how can value of look not look self

you have utilize ng-href (instead of href) in template:

<div class="span2"> <a ng-href="#/cat/{{cat.id}}"> link cat</a> </div>

and wrap getval() method in $timeout retrieve href attribute after digest cycle:

$timeout(function() { element.getval(); })

ps: don't know trying accomplish reading attributes values template scope not best practice. improve thought build href attribute in directive utilize straight in view (like scope.carurl = '#/cat/' + cat.id' , <a ng-href="{{caturl}}">)

javascript jquery angularjs angularjs-directive

No comments:

Post a Comment