create account

Insert / Detach ViewRefs between multiple ViewContainerRefs by azarus

View this thread on: hive.blogpeakd.comecency.com
· @azarus · (edited)
$21.75
Insert / Detach ViewRefs between multiple ViewContainerRefs
I submitted a bug report and a feature request on the link below: 
https://github.com/angular/angular/issues/20824
Github issue can be found here:

The bug occurs when you try to detach * attach items between ViewContainers in angular.
The bug is caused by a missing reparenting code in the angular core. I was not sure the nature of the problem it might have been inteded so that's why it is also a feature request.


## The Bug / Current behavior
We are unable to detach a ViewRef from **ViewContainerRef A** and attach it to a different **ViewContainerRef B**

Insert / Detaching doesn't trigger change detection on both ViewContainers.
Using `@ContentChildren` fails to update changes properly.

## Expected behavior
I would like to be able to detach a ViewRef and attach it to a new ViewContainer without recreating the components inside the view. This should work without any issues.

## Minimal reproduction of the problem with instructions
https://stackblitz.com/edit/angular-gitter-couoxk?file=app%2Ftest.ts

Manual parent attachment, interaction with underlying dom api is required. Uncomment the code in `test.ts` **LINE 118** to fix to see a workaround & expected behavior.

The provided attachment logic seems to be missing from angular core located here:
https://github.com/angular/angular/blob/5.1.x/packages/core/src/view/refs.ts#L192

## What is the motivation / use case for changing the behavior?
I have created a multi drag & drop library in angular, and this bug prevented me using the built in *ngFor due to errors being printed out in the console. Fixing this would help fellow developers to implement such a drag & drop without a hassle and without digging through the angular core codebase.

This would allow to implement Drag & Drop that doesn't recreate the  components when an item is dragged over.
Would allow to manipulate dom without using nativeElement, No more worrying about *ngFor trying to find the changes and throwing errors when nodes are missing in dom.

## Possible solution
Implement public api for ``*ngFor`` for detaching & attaching views between ViewContainers
```typescript
	attachView(view: ViewRef, index: number)
	{
		let template = (<any>this._template);
		let parentNodeDef = template._def;
		let parent = template._parentView;


 		// The Important part!!!
		let viewRef = (<any>view);
		viewRef._view.root = parent.root;
		viewRef._view.parent = parent;
		viewRef._view.renderer = parent.renderer;
		viewRef._view.parentNodeDef = template._def;
		viewRef._view.state = 1 << 0;

		this.ViewContainer.insert(viewRef, index);
	
	}
```
The bug i caused by a missing marent assignment.l when reattaching the view to a new container. This assignment only occurs when the view is created.
```typescript
viewRef._view.parent = parent;
```
## Environment

<pre><code>
Angular version: 4.x
and 
Angular version: 5.x
<!-- Check whether this is still an issue in the most recent Angular version -->

Browser:

- [x] Chrome (desktop) version XX
- [x] Chrome (Android) version XX
- [x] Chrome (iOS) version XX
- [x] Firefox version XX
- [x] Safari (desktop) version XX
- [x] Safari (iOS) version XX
- [x] IE version XX
- [x] Edge version XX

 - Platform:  Mac, linux, Windows


<br /><hr/><em>Posted on <a href="https://utopian.io/utopian-io/@azarus/insert-detach-viewrefs-between-multiple-viewcontainerrefs">Utopian.io -  Rewarding Open Source Contributors</a></em><hr/>
👍  , , , , , , , , , , , , ,
properties (23)
authorazarus
permlinkinsert-detach-viewrefs-between-multiple-viewcontainerrefs
categoryutopian-io
json_metadata"{"links":["https://github.com/angular/angular/issues/20824","https://stackblitz.com/edit/angular-gitter-couoxk?file=app%2Ftest.ts","https://github.com/angular/angular/blob/5.1.x/packages/core/src/view/refs.ts#L192","https://utopian.io/utopian-io/@azarus/insert-detach-viewrefs-between-multiple-viewcontainerrefs"],"image":[],"tags":["utopian-io","angular","bug-report","programming","code"],"app":"utopian/1.0.0","format":"markdown+html","community":"utopian","type":"bug-hunting","repository":{"score":75.82821,"default_branch":"master","watchers":31539,"open_issues":2058,"forks":7829,"license":{"url":"https://api.github.com/licenses/mit","spdx_id":"MIT","name":"MIT License","key":"mit"},"open_issues_count":2058,"archived":false,"mirror_url":null,"forks_count":7829,"has_pages":false,"has_wiki":false,"has_downloads":true,"has_projects":true,"has_issues":true,"language":"TypeScript","watchers_count":31539,"stargazers_count":31539,"size":70915,"homepage":"https://angular.io","svn_url":"https://github.com/angular/angular","clone_url":"https://github.com/angular/angular.git","ssh_url":"git@github.com:angular/angular.git","git_url":"git://github.com/angular/angular.git","pushed_at":"2018-01-02T05:56:43Z","updated_at":"2018-01-02T06:56:55Z","created_at":"2014-09-18T16:12:01Z","deployments_url":"https://api.github.com/repos/angular/angular/deployments","releases_url":"https://api.github.com/repos/angular/angular/releases{/id}","labels_url":"https://api.github.com/repos/angular/angular/labels{/name}","notifications_url":"https://api.github.com/repos/angular/angular/notifications{?since,all,participating}","milestones_url":"https://api.github.com/repos/angular/angular/milestones{/number}","pulls_url":"https://api.github.com/repos/angular/angular/pulls{/number}","issues_url":"https://api.github.com/repos/angular/angular/issues{/number}","downloads_url":"https://api.github.com/repos/angular/angular/downloads","archive_url":"https://api.github.com/repos/angular/angular/{archive_format}{/ref}","merges_url":"https://api.github.com/repos/angular/angular/merges","compare_url":"https://api.github.com/repos/angular/angular/compare/{base}...{head}","contents_url":"https://api.github.com/repos/angular/angular/contents/{+path}","issue_comment_url":"https://api.github.com/repos/angular/angular/issues/comments{/number}","comments_url":"https://api.github.com/repos/angular/angular/comments{/number}","git_commits_url":"https://api.github.com/repos/angular/angular/git/commits{/sha}","commits_url":"https://api.github.com/repos/angular/angular/commits{/sha}","subscription_url":"https://api.github.com/repos/angular/angular/subscription","subscribers_url":"https://api.github.com/repos/angular/angular/subscribers","contributors_url":"https://api.github.com/repos/angular/angular/contributors","stargazers_url":"https://api.github.com/repos/angular/angular/stargazers","languages_url":"https://api.github.com/repos/angular/angular/languages","statuses_url":"https://api.github.com/repos/angular/angular/statuses/{sha}","trees_url":"https://api.github.com/repos/angular/angular/git/trees{/sha}","git_refs_url":"https://api.github.com/repos/angular/angular/git/refs{/sha}","git_tags_url":"https://api.github.com/repos/angular/angular/git/tags{/sha}","blobs_url":"https://api.github.com/repos/angular/angular/git/blobs{/sha}","tags_url":"https://api.github.com/repos/angular/angular/tags","branches_url":"https://api.github.com/repos/angular/angular/branches{/branch}","assignees_url":"https://api.github.com/repos/angular/angular/assignees{/user}","events_url":"https://api.github.com/repos/angular/angular/events","issue_events_url":"https://api.github.com/repos/angular/angular/issues/events{/number}","hooks_url":"https://api.github.com/repos/angular/angular/hooks","teams_url":"https://api.github.com/repos/angular/angular/teams","collaborators_url":"https://api.github.com/repos/angular/angular/collaborators{/collaborator}","keys_url":"https://api.github.com/repos/angular/angular/keys{/key_id}","forks_url":"https://api.github.com/repos/angular/angular/forks","url":"https://api.github.com/repos/angular/angular","fork":false,"description":"One framework. Mobile & desktop.","html_url":"https://github.com/angular/angular","private":false,"owner":{"site_admin":false,"type":"Organization","received_events_url":"https://api.github.com/users/angular/received_events","events_url":"https://api.github.com/users/angular/events{/privacy}","repos_url":"https://api.github.com/users/angular/repos","organizations_url":"https://api.github.com/users/angular/orgs","subscriptions_url":"https://api.github.com/users/angular/subscriptions","starred_url":"https://api.github.com/users/angular/starred{/owner}{/repo}","gists_url":"https://api.github.com/users/angular/gists{/gist_id}","following_url":"https://api.github.com/users/angular/following{/other_user}","followers_url":"https://api.github.com/users/angular/followers","html_url":"https://github.com/angular","url":"https://api.github.com/users/angular","gravatar_id":"","avatar_url":"https://avatars3.githubusercontent.com/u/139426?v=4","id":139426,"login":"angular"},"full_name":"angular/angular","name":"angular","id":24195339},"platform":"github","pullRequests":[],"moderator":{"account":"wehmoen","reviewed":true,"pending":false,"flagged":false}}"
created2018-01-02 08:31:12
last_update2018-01-05 19:25:51
depth0
children5
last_payout2018-01-09 08:31:12
cashout_time1969-12-31 23:59:59
total_payout_value15.394 HBD
curator_payout_value6.358 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length3,411
author_reputation4,239,423,105,752
root_title"Insert / Detach ViewRefs between multiple ViewContainerRefs "
beneficiaries
0.
accountutopian.pay
weight2,500
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id26,516,365
net_rshares2,238,656,925,484
author_curate_reward""
vote details (14)
@gnchmurat ·
upvoted and followed...
👍  
properties (23)
authorgnchmurat
permlinkre-azarus-insert-detach-viewrefs-between-multiple-viewcontainerrefs-20180102t094304155z
categoryutopian-io
json_metadata{"tags":["utopian-io"],"app":"steemit/0.1"}
created2018-01-02 09:43:03
last_update2018-01-02 09:43:03
depth1
children0
last_payout2018-01-09 09:43:03
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length23
author_reputation1,296,965,930,680
root_title"Insert / Detach ViewRefs between multiple ViewContainerRefs "
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id26,525,735
net_rshares74,243,838
author_curate_reward""
vote details (1)
@netuoso ·
This is precisely the reason Utopian was created.

I will be interested to see how much code was submitted to open source projects around the globe through Utopian.
👍  
properties (23)
authornetuoso
permlinkre-azarus-insert-detach-viewrefs-between-multiple-viewcontainerrefs-20180102t191337218z
categoryutopian-io
json_metadata{"tags":["utopian-io"],"app":"steemit/0.1"}
created2018-01-02 19:13:36
last_update2018-01-02 19:13:36
depth1
children1
last_payout2018-01-09 19:13:36
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length164
author_reputation151,901,967,807,285
root_title"Insert / Detach ViewRefs between multiple ViewContainerRefs "
beneficiaries
0.
accountsteemliberator
weight500
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id26,619,047
net_rshares229,362,078
author_curate_reward""
vote details (1)
@azarus ·
Oh there will be a lot from me for sure. I've been programming for almost 10 years now. I do have lots of code, libraries, frameworks, snippets and knowledge to share. I just couldn't afford the time until know. Thanks for letting me be here :)
properties (22)
authorazarus
permlinkre-netuoso-re-azarus-insert-detach-viewrefs-between-multiple-viewcontainerrefs-20180104t031036381z
categoryutopian-io
json_metadata{"tags":["utopian-io"],"app":"steemit/0.1"}
created2018-01-04 03:10:36
last_update2018-01-04 03:10:36
depth2
children0
last_payout2018-01-11 03:10:36
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length244
author_reputation4,239,423,105,752
root_title"Insert / Detach ViewRefs between multiple ViewContainerRefs "
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id26,928,382
net_rshares0
@utopian-io ·
### Hey @azarus I am @utopian-io. I have just upvoted you!
#### Achievements
- You have less than 500 followers. Just gave you a gift to help you succeed!
- Seems like you contribute quite often. AMAZING!
#### Suggestions
- Contribute more often to get higher and higher rewards. I wish to see you often!
- Work on your followers to increase the votes/rewards. I follow what humans do and my vote is mainly based on that. Good luck!
#### Get Noticed!
- Did you know project owners can manually vote with their own voting power or by voting power delegated to their projects? Ask the project owner to review your contributions!
#### Community-Driven Witness!
I am the first and only Steem Community-Driven Witness. <a href="https://discord.gg/zTrEMqB">Participate on Discord</a>. Lets GROW TOGETHER!
- <a href="https://v2.steemconnect.com/sign/account-witness-vote?witness=utopian-io&approve=1">Vote for my Witness With SteemConnect</a>
- <a href="https://v2.steemconnect.com/sign/account-witness-proxy?proxy=utopian-io&approve=1">Proxy vote to Utopian Witness with SteemConnect</a>
- Or vote/proxy on <a href="https://steemit.com/~witnesses">Steemit Witnesses</a>

[![mooncryption-utopian-witness-gif](https://steemitimages.com/DQmYPUuQRptAqNBCQRwQjKWAqWU3zJkL3RXVUtEKVury8up/mooncryption-s-utopian-io-witness-gif.gif)](https://steemit.com/~witnesses)

**Up-vote this comment to grow my power and help Open Source contributions like this one. Want to chat? Join me on Discord https://discord.gg/Pc8HG9x**
properties (22)
authorutopian-io
permlinkre-azarus-insert-detach-viewrefs-between-multiple-viewcontainerrefs-20180105t213839241z
categoryutopian-io
json_metadata{"tags":["utopian-io"],"community":"utopian","app":"utopian/1.0.0"}
created2018-01-05 21:38:39
last_update2018-01-05 21:38:39
depth1
children0
last_payout2018-01-12 21:38:39
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length1,504
author_reputation152,955,367,999,756
root_title"Insert / Detach ViewRefs between multiple ViewContainerRefs "
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id27,375,918
net_rshares0
@wehmoen · (edited)
$0.14
 
👍  
properties (23)
authorwehmoen
permlinkre-azarus-insert-detach-viewrefs-between-multiple-viewcontainerrefs-20180105t150435572z
categoryutopian-io
json_metadata{}
created2018-01-05 15:04:36
last_update2020-06-03 07:02:06
depth1
children0
last_payout2018-01-12 15:04:36
cashout_time1969-12-31 23:59:59
total_payout_value0.104 HBD
curator_payout_value0.031 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length1
author_reputation86,504,380,503,125
root_title"Insert / Detach ViewRefs between multiple ViewContainerRefs "
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id27,302,807
net_rshares14,033,236,322
author_curate_reward""
vote details (1)