referencing objects with a lot of fields

91 views
Skip to first unread message

Tamas Herman

unread,
Mar 26, 2012, 3:36:54 AM3/26/12
to ang...@googlegroups.com
guys,

im trying to display an object with 10-20 attributes.
some of the values of these attributes are also similarly big objects or arrays of such objects.

i would like to omit prefixing each field name with the sub-object's name.
similarly as knockoutjs does in their example on the homepage:

<p data-bind="with: chosenTicket">
xxx <b data-bind="text: name"></b>

pascal had a similar construct which was also called "with"

so how can i achieve something like this in angular:

$scope.obj = {
f1: 123123
f2: "asdasd"

f13: {
sub1: true
sub2: 555

sub20: null
}
f18: [ {...}, {…}, ... ]
}

<p with="obj">
<h3>{{f1}}</h3>
<p>{{f2}}<p>
...
<div with="f13">
Done: {{sub1}}
Size: {{sub2}}

<span ng-show="sub20">OK</span>
</div>
...
</p>

instead of the current

<p>
<h3>{{obj.f1}}</h3>
<p>{{obj.f2}}<p>
...
<div with="obj.f13">
Done: {{obj.f13.sub1}}
Size: {{obj.f13.sub2}}

<span ng-show="obj.f13.sub20">OK</span>
</div>
...
</p>

i was trying to hack it like this:

<script type="text/ng-template" id="/embedded.html">
<div>
Done: {{sub1}}
Size: {{sub2}}

<span ng-show="sub20">OK</span>
</div>


</script>

<div ng-inculde src="'/embedded.html'" scope="extend($new, obj.f13)"></div>

but it didnt do anything.
didn't throw any error either which made me feel good about it actually :)

i have the impression that i really read the full docs…
would these sections make me understand whats going on?
http://docs-next.angularjs.org/api/angular.module.ng.$rootScope.Scope
http://docs-next.angularjs.org/api/angular.module.ng.$compileProvider.directive

is it silly for some reason what i want?
pls, help.

--
tom


Peter Bacon Darwin

unread,
Mar 26, 2012, 4:14:06 AM3/26/12
to ang...@googlegroups.com
Hi Tamas
You can get this effect by extending the scope inside a controller.  For each new controller a new scope is created that exists only where that controller exists so you are free to hack it to bits as long as you are sure that is what you want to do.
See this fiddle: http://jsfiddle.net/86kuc/
Pete



--
You received this message because you are subscribed to the Google Groups "AngularJS" group.
To post to this group, send email to ang...@googlegroups.com.
To unsubscribe from this group, send email to angular+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/angular?hl=en.


Vojta Jína

unread,
Mar 27, 2012, 12:47:08 AM3/27/12
to ang...@googlegroups.com
Pete, thank you so much for answering all the questions here on mailing list.
We really appreciate it.

Would you like to have an AngularJS t-shirt ?
Fill in this form
https://docs.google.com/spreadsheet/viewform?formkey=dGlWVFNocm9hWFF0Z3htVUZkNTFqOVE6MQ
and we will send you some...

V.

Suller Andras

unread,
Mar 27, 2012, 4:03:38 AM3/27/12
to ang...@googlegroups.com
On Mon, Mar 26, 2012 at 3:14 PM, Peter Bacon Darwin
<pe...@bacondarwin.com> wrote:
> Hi Tamas
> You can get this effect by extending the scope inside a controller.  For
> each new controller a new scope is created that exists only where that
> controller exists so you are free to hack it to bits as long as you are sure
> that is what you want to do.
> See this fiddle: http://jsfiddle.net/86kuc/
> Pete


Interesting approach. But (of course) it breaks the 2way databinding,
since the attributes will be copied to the new scope. Is there any
chance to achieve the same effect without making new copies of the
data?
Or try to keep the two copies in sync with a $watch for every
attribute? It sounds too hackish.

Andras

Peter Bacon Darwin

unread,
Mar 27, 2012, 4:36:11 AM3/27/12
to ang...@googlegroups.com
Copies are only made of primitive types.  If you are willing to accept one level of indirection then you get references to objects.
See this version: http://jsfiddle.net/86kuc/1/
Pete

Peter Bacon Darwin

unread,
Mar 27, 2012, 4:36:40 AM3/27/12
to ang...@googlegroups.com
Awesome and wholly undeserved - but I'll take one anyway!
Thanks
Pete

Igor Minar

unread,
Mar 29, 2012, 2:43:26 AM3/29/12
to ang...@googlegroups.com
On Tue, Mar 27, 2012 at 1:03 AM, Suller Andras <suller...@gmail.com> wrote:
On Mon, Mar 26, 2012 at 3:14 PM, Peter Bacon Darwin
<pe...@bacondarwin.com> wrote:
> Hi Tamas
> You can get this effect by extending the scope inside a controller.  For
> each new controller a new scope is created that exists only where that
> controller exists so you are free to hack it to bits as long as you are sure
> that is what you want to do.
> See this fiddle: http://jsfiddle.net/86kuc/
> Pete


Interesting approach. But (of course) it breaks the 2way databinding,
since the attributes will be copied to the new scope. Is there any
chance to achieve the same effect without making new copies of the
data?

There isn't another way. 
 
Or try to keep the two copies in sync with a $watch for every
attribute? It sounds too hackish.

I wouldn't recommend that.

What's your objective? Saving key strokes or something else?

I'm all for saving key strokes but in this case, it would add yet another scoping dimension to the templating syntax which might make it hard to follow.

/i
Reply all
Reply to author
Forward
0 new messages