Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass object to directives without watching them

Generally we use the = to make two-way data binding when creating directives, for example:

app.module('main')
    .directive('dirTest'[function(){
        return {
            scope:{
                data:'='
            }
        }
    }])

Then we can use

<dir-test data="xx.data"></dir-test>

To pass the xx.data object to the directive, but it seems that this will make angular watch this expression.

This may be a waste of time once I do not care the change of the data or the data can hardly change.

Which means I need a one-way one-time data passwed to the directive, is this possible?


I ask this question because when I open the Batarang console, I found the regularinterceptedexpression cost too long and watch a lot of watchers. I think the page may run fast if I can remove some unused watchers.

I know there is a one-way from dom to directives by using @, however it seems that it just support string.

like image 698
hguser Avatar asked Jan 30 '26 02:01

hguser


1 Answers

In angularjs if you create a directive with scope '@' then it will be Text bindings,which means you will be able to get the value of expression but making changes to it won't reflect back to the html page.so i believe if you create the directive in with scope @ then it will do a job for you.

app.module('main')
    .directive('dirTest'[function(){
        return {
            scope:{
                data:'@'
            }
        }
    }]

)

<dir-test data={{xx.data}}></dir-test>
like image 121
maddygoround Avatar answered Feb 01 '26 18:02

maddygoround