Friday, 15 August 2014

javascript - AngularJS best practice to inherit scope in directive -



javascript - AngularJS best practice to inherit scope in directive -

from directives documentation says inherit scope in 2 ways:

@ or @attr - bind local scope property value of dom attribute. result string since dom attributes strings. if no attr name specified attribute name assumed same local name. given , widget definition of scope: { localname:'@myattr' }, widget scope property localname reflect interpolated value of hello {{name}}. name attribute changes localname property on widget scope. name read parent scope (not component scope).

= or =attr - set bi-directional binding between local scope property , parent scope property of name defined via value of attr attribute. if no attr name specified attribute name assumed same local name. given , widget definition of scope: { localmodel:'=myattr' }, widget scope property localmodel reflect value of parentmodel on parent scope. changes parentmodel reflected in localmodel , changes in localmodel reflect in parentmodel.

considering want pass elementid, id, can pass through =elementid or @elementid.

now 1 of 2 considered best practice? if utilize @ , hence attribute, take value dom slower taking straight value?

am correct? suggestions?

if directive needs alter value, utilize = bi-directional binding. if want create clear other people reading code using one-way binding, utilize @. if need access value in linking function synchronously, utilize = (see note @ , $observe's asynchronous behavior here).

for case, @ seems best. (an illustration of how utilize directive , elementid in html element helpful though.)

i have no thought slower/faster.

see what difference between '@' , '=' in directive scope

javascript angularjs

No comments:

Post a Comment