I'm a AngularJS newbie.
I am looking to pass data when loading partials.
I'd like to have a directive that would allow for dynamic attributes. For example, in a view where an object "company" is defined on the scope I could have:
<partial url:"employees/index.html" query_string: "Fun House Inc" employees="company.employees">
and in a view where the object "employee" is defined:
<partial url:"employees/show.html" employee="employee">
Below is a directive I have written (in coffeescript) that almost works. In the above example the query_string attribute gets passed (thanks to the scope[k] = v in the link function) but the employees attribute does not namely it passes the string not the evaluated object.
App.directive "partial", ->
restrict: "E"
scope:
attribute: "@"
expression: "&"
model: "="
array: "="
data: "="
templateUrl: (el,attrs)->
"/assets/partials/" + attrs.url
link: (scope,el,attrs)->
for k, v of attrs
scope[k] = v if (k[0] != "$" && !scope[k]?)
In this directive I have defined array, model, and data so I can hack around my problem by doing something like
<partial url:"employees/index.html" query_name: "Fun House Inc" array="company.employees">
and
<partial url:"employees/show.html" model="employee">
in my views. That said I am not super happy with this solution and I am surprised there isn't a ready made solution out there. Am I going about this all wrong? Is there a better way to do this, or a way to fix my directive so that it works the way I want it to?