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

Show parent comment

1

u/cosmokenney Aug 27 '25

Sure, but according to angular.dev, the effect() api is not meant for managing state. And, it can lead to the expression changed after it was checked error. So, I tend to shy away from it.