Skip to content
This repository was archived by the owner on Feb 22, 2018. It is now read-only.

Getting type error from ng_repeat.dart #1089

Closed
yjbanov opened this issue May 31, 2014 · 15 comments
Closed

Getting type error from ng_repeat.dart #1089

yjbanov opened this issue May 31, 2014 · 15 comments

Comments

@yjbanov
Copy link
Contributor

yjbanov commented May 31, 2014

The type on the closure is wrong

https://github.com/angular/angular.dart/blob/a1d67aa9442a29b928950856f662b046468a6d5b/lib/directive/ng_repeat.dart#L134

@vicb
Copy link
Contributor

vicb commented Jun 2, 2014

Could you please give more details about this ?

It should get a CollectionChangeRecord when you are watching an iterable.

@yjbanov
Copy link
Contributor Author

yjbanov commented Jun 2, 2014

@chirayuk and @jbdeboer have the details, but basically this happens when the model bound to ng-repeat changes type from List to something other than a List. My situation goes roughly like this:

<div ng-if="ctrl.isList">
  <div ng-repeat="item in ctrl.data">{{item}}</div>
</div>
<div ng-if="!ctrl.isList">
  <div>{{ctrl.data}}</div>
</div>

When the type of ctrl.data changes, Angular still tries to evaluate ng-repeat incorrectly assuming ctrl.data is a list, when in fact it no longer is.

@vicb
Copy link
Contributor

vicb commented Jun 2, 2014

That's something we can fix in angular.dart but it could also be fixed in client code:
item in ctrl.getList() - where getList() would return null or const [], that would be more dartish

@yjbanov
Copy link
Contributor Author

yjbanov commented Jun 2, 2014

I don't understand your solution. Could you please use it on my example above?

@matanlurey
Copy link

Vic is suggesting in the ctrl.isList case, you use "item in ctrl.getList()" instead of accessing data directly, and have the getList() function do a guard and either return a null (or const[]) if the data is not of type List.

@vicb
Copy link
Contributor

vicb commented Jun 3, 2014

that's exactly what I was suggesting, let me know if anything is still unclear

@yjbanov
Copy link
Contributor Author

yjbanov commented Jun 3, 2014

Good workaround. Should still be fixed in Angular.

@vicb
Copy link
Contributor

vicb commented Jun 3, 2014

Should still be fixed in Angular.

What do other thinks ? I would not say this is a bug

@yjbanov
Copy link
Contributor Author

yjbanov commented Jun 3, 2014

How is the exception "type 'String' is not a subtype of type 'CollectionChangeRecord' of 'changes'" deep inside angular code not a bug?

@vicb
Copy link
Contributor

vicb commented Jun 3, 2014

You're right, this is a bug, we have two ways to fix it:

  1. remove all rows when the type is not a CollectionChangeRecord,
  2. throw a clearer exception.

To me getting someting other than a CollectionChangeRecord indicates a wrong usage and we should throw an exception, so I'm in favor of 2.

Thoughts ?

@yjbanov
Copy link
Contributor Author

yjbanov commented Jun 3, 2014

Could you describe what it is that's wrong with the usage?

@vicb
Copy link
Contributor

vicb commented Jun 3, 2014

The code is trying to iterate (ng-repeat) over a String while it can only iterate over a collection (List and Iterator IIRC)

@yjbanov
Copy link
Contributor Author

yjbanov commented Jun 3, 2014

If you look at the example above, the user's code is not trying to iterate over a String, because ng-repeat is guarded by ng-if.

@vicb
Copy link
Contributor

vicb commented Jun 3, 2014

If you look at the example above, the user's code is not trying to iterate over a String, because ng-repeat is guarded by ng-if.

This must be due to the evaluation order that you can not control which is why a think throwing "ng-repeat can not iterate over a 'String'" could help.

Notes:

  • It is not very Dartish to put either a List or a String into the same variable,
  • I'll take a look at the details for why ng-repeat is evaluated even if the condition is false.

@vicb
Copy link
Contributor

vicb commented Jun 8, 2014

@yjbanov I'm closing this issue as the last refactoring done for ng-repeat supports your use case (it would remove all rows when the subject is not Iterable).

I'll create a separate issue to investigate why ng-repeat is evaluated when it should not.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Development

No branches or pull requests

3 participants