I hope that this little tip would help save you the couple
of hours that it took me to figure out.
I have an Angular2 component with a View that has two-way
data binding to a TypeScript object model.
The component calls a Web API Service to populate and then update this object
model. The TypeScript model in the Angular2 component is identical to the strongly-typed Data Model in the Web API. But when I subscribe to the Web API Service
and then assign its result to my object model, the properties of my object model
become “Undefined”.
This is my Angular2 component (abbreviated for
brevity):
This is my Angular2 view (abbreviated for brevity):
This is my TypeScript model for the UserAccounts object:
This is my implementation of the "updateUserAccount" service
in the Web API, with some Swagger annotations:
This Http PUT operation is returning a data object of type “UserAccount”.
And this is my strongly-typed UserAccount class in my Web
API:
As you can see, the data models in the Angular2 client
component and the Web API server response are identical.
In fact, at the "ngOnInit" event handler, the call to the “getUser”
service is successful, returning a response of type “UserAccountModel”, and
populating the view with the object data accordingly.
This is what the view looks like after “ngOnInit”:
The “Update” event handler is nearly identical. It makes a call to the “updateUserAccount”
service of the Web API, which is an Http PUT verb operation. The “profileInput” parameter is of type “UserAccountModel”,
and the API service return type is also “UserAccountModel”.
However, once execution enters this “Update” event handler,
the properties of the “profileInput” object become "Undefined". Assigning the property values of the return
object to the individual properties of the “profileInput” object did not help
either.
I have verified that the Web API service was returning a
response successfully. Yet the two-way
bound properties of the data object were becoming Undefined. So what the heck is going on?
It turns out that, unlike the return objects from my Web API’s
GET operations, the return objects from my Web API’s PUT operations undergo a
quirky transformation. Specifically the
casing of their property names is automatically changed to Pascal Case, so that
the first letter of each property name becomes lowercase.
Below is what a similar object would look like in Google’s
Developer Tools. Notice that the
property names are in Pascal Case, whereas the derived data model was defined
with Camel Case. That incongruity caused
the property assignments to the “profileInput” object in the Angular2 client
component to fail.
I had to create a new TypeScript data model that observed
this new casing, and use that as the return data type from the Web API call.
Replacing the UserAccountModel with the
UserAccountReturnModel as the return type of the “updateUserAccount” service
call, everything began to work as expected.
The ngModel two-way data binding does not really have any direct
relevance to the quirk with the return object from the Web API, but I did
mention it in this article because the symptoms of this issue might be confused
with the two-way data binding with the data model during an HTTP call.
In this blog, I showed a “gotcha” when making HTTP service
calls and the object types returned from those calls.