angularjs - Utility functions for directives -
say want make angular directive generates links resources this:
<a href="http://link/to/resource/1234">link/to/resource/1234</a>
from object looks like:
resource = { id: 1234, otherproperty: 'foo' }
how can w/ directive? ie, i'd not have repeat part goes '/link/to/resource/{{id}}'
. can't seem work right. 1 of several things i've tried:
app.directive('myresource', function() { return { restrict: 'e', scope: { resource: '=' }, baseurl: 'link/to/{{resource.id}}', template: '<a href="http://{{baseurl}}">{{baseurl}}</a>' }; });
which ends rendering:
<a href="http://" class="ng-binding"></a>
other things i've tried (like making baseurl
function/sticking in scope
) have resulted in similar things/errors.
is there way work?
one way handle use directive's link function set variable you, this:
link: function(scope) { scope.baseurl= 'link/to/'+scope.resource.id; }, template: '<a href="http://{{baseurl}}">{{baseurl}}</a>'
here's working fiddle
alternatively use approach:
link: function(scope) { scope.baseurl= 'link/to/'; }, template: '<a href="http://{{baseurl}}{{resource.id}}">{{baseurl}}{{resource.id}}</a>
here's fiddle
Comments
Post a Comment