Angularjs: is this the correct way to use controller and directive? -


normally directive use data in scope, example:

<div my-directive       data-x='x'       data-y='y'      data-z='z'      ...> </div> 

however, arguments of directive grow fast. put them in model class. each of directive has it's own model class.

<div my-directive       data-model='mydirectivemodel'> </div> 

in controllers, need set

$scope.mydirectivemodel = {      x: 100.     y: 200,     add: function(){        return this.x + this.y;     } }; 

is correct way use controller , directive?

it's cleaner way of passing data through directive having lots attributes in markup.

however suggest 1 of things angular it's declarative syntax. if , exclusively you've suggested, when come read markup in couple of months you'll have delve code see what's going on. being more explicit in markup save time. example when this:

<div my-directive show-chickens="yes" chimp-count="monkeys" /> 

less hidden when read markup.

however, if happy approach , want continue, declare model value of directive attribute so...

<div my-directive='mydirectivemodel' /> 

just make more clean.


Comments

Popular posts from this blog

php - Submit Form Data without Reloading page -

linux - Rails running on virtual machine in Windows -

php - $params->set Array between square bracket -