Angular JS - Is there a way to pass the directive's attribute to the template

on the Page

<rn-text-edit rn-scope="profile.first_name"></rn-text-edit>

on the js

app.directive("rnTextEdit", function () {
    return {
        restrict: 'E',
        replace: true,
        template:'<span>{{'+rn-scope+'}}</span>'
    }
});

I know I can replace the DOM and access the attribute through link. I wonder if there is a way of passing the directive's attribute to a template.

Answers


If you are just displaying the value:

<rn-text-edit rn-scope="{{profile.first_name}}"></rn-text-edit>

-

app.directive("rnTextEdit", function () {
    return {
        restrict: 'E',
        replace: true,
        scope: {
            rnScope: '@'
        },
        template: '<span>{{rnScope}}</span>'
    }
});

If the directive needs to modify the value, you could use '=' and skip the double curlies.

fiddle

more info on scope and '@' in the Angular Directives page


Need Your Help

Running a proxi server in Rails3

ruby-on-rails ruby-on-rails-3 apache rack mongrel

Since rails does not have a mechanism for multi-threading, what is the common good-practice of handling multiple requests at the same time?

How can I remove Spry Validation from a Radio Group?

javascript spry

I understand how to destroy validation for a text field, but when I apply the same logic to a radio button group, the Spry Validation doesn't destroy.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.