I am using angularJs
and have a field stored in MSSQL
as varchar(10)
. The data is stored and displayed correctly so long as it isn't a 4 character number like 1234
.
When I save that string, the db has the correct value of 1234
, but when it is displayed in the UI, it looks like a date: "1234-01-01T00:00:00.000Z"
The html uses {{project.PurchaseOrderNumber}}
to render the data.
angularJs
code:
projectService.getProjects().then(function (response) {
$scope.projects = response.data;
$scope.loading = false;
}, function() {
messageService.showError("Failed to get projects. Please refresh and try again");
});
service.getProjects = function() {
return $http.get(baseUrl + 'API/Projects/');
};
C#
code:
[Route("")]
public List<Project> GetProjects()
{
return _projectService.GetProjects(_companyId.ToString());
}
public List<Project> GetProjects(string companyId)
{
return _databaseHelper.QueryProc<Project>("projects_get", new { companyId }).ToList();
}
public class Project
{
public Guid Id { get; set; }
public Guid CompanyId { get; set; }
public string Status { get; set; }
public string PurchaseOrderNumber { get; set; }
}
JSON
string as part of the response:
[{
"Id": "daef5b27-fb8c-4804-a407-0165e17c3a60",
"CompanyId": "76bca824-6e93-45a5-bc5a-ba20d030ebce",
"Status": "New",
"PurchaseOrderNumber": "1234-01-01T00:00:00.000Z"
}, {
"Id": "09d87946-dec2-4721-bf71-df436919b599",
"CompanyId": "76bca824-6e93-45a5-bc5a-ba20d030ebce",
"Status": "New",
"PurchaseOrderNumber": "160728"
}]
MSSQL
code:
SELECT [id]
,[companyId]
,[status]
,[purchaseOrderNumber]
FROM [dbo].[projects]
I have confirmed that prior to GetProjects
returning the results, the purchaseOrderNumber
is still 1234
. However, when I break at $scope.projects = response.data;
it is in date format.
UPDATE
I just checked the Network tab in Chrome and see that the data is actually in the correct format, but when it is provided to response.data
it isn't.
4:{Id: "daef5b27-fb8c-4804-a407-0165e17c3a60", CompanyId: "76bca824-6e93-45a5-bc5a-ba20d030ebce",…}
CompanyId:"76bca824-6e93-45a5-bc5a-ba20d030ebce"
Id:"daef5b27-fb8c-4804-a407-0165e17c3a60"
PurchaseOrderNumber:"1234"
Status:"New"
UPDATE 2
I have learned that this happens with any field. If it contains only 4 digits (any 4 digits, in any order), it is rendered as a date when it is provided to angularJs
Why does angular do this? How do I stop it?