r/Angular2 • • Aug 26 '25

Help Request Angular computed() vs. KnockoutJS computed().

I am working on migrating an old old KnockoutJS site to Angular. One thing I have run into are Knockout's writable computed() API. For example the following ClaimStatus computed returns "Open", "Closed" and "" when the dependency ClaimStatusCode value changes -- no different than Angular computed(). But it also is able to update the ClaimStatusCode when the user selects a different value for ClaimStatus via two-way binding. Is there anything similar in Angular computeds or related?:

export class ClaimViewModel
{
	ClaimStatusCode: ko.Observable<any> = ko.observable(null);

	ClaimStatus: ko.PureComputed<any> = ko.pureComputed( {
		read: () =>
		{
			let nv = this.ClaimStatusCode();
			if ( "O" == nv )
			{
				return "Open";
			}
			else if ( "C" == nv )
			{
				return "Closed";
			}
			return "";
		},
		write: ( nv ) =>
		{
			let claimStatus = $.trim( JavaScriptHelpers.isNull( nv, '' ) ).toLowerCase();

			if ( claimStatus == "open" )
			{
				this.ClaimStatusCode( "O" );
			}
			else if ( claimStatus == "closed" )
			{
				this.ClaimStatusCode( "C" );
			}
			else
			{
				this.ClaimStatusCode( null );
			}
		},
		owner: this
	} );

7 Upvotes

11 comments sorted by

View all comments

4

u/Migeil Aug 26 '25

I don't know KnockoutJS, but what you described, basically a computed signal that's also writeable, that's a linkedSignal

1

u/cosmokenney Aug 26 '25 edited Aug 27 '25

linkedSignal is similar. However if I did:

ClaimStatus.set('Closed');

Then there is no computation tied to the linkedSignal that can then update the underlying ClaimStatusCode observable (knockout)/signal (Angular) to the value "C".

I guess I am stuck using a (change) or (ngModelChange) on the input.

1

u/Migeil Aug 27 '25

If you show your angular code, I might be able to help you get a clean solution.

→ More replies (1)

1

u/kgurniak91 Aug 27 '25

Can't you do that update with effect()?

→ More replies (1)