Integration of Razor and AngularJS (2)
January 12, 2014 11:40I found the technique I showed in my previous post particularly useful when you need to "migrate" some fields from server-side to client-side. By "migrate" I mean: make fields which were initially only needed on the server side, for page rendering, available on the client-side, in order to access and manipulate them through JavaScript too (for user interaction through Angular, in this case).
Suppose you have a page to display some person details:
All is well, until you decide that you want to provide a way to edit the description. You do so by adding a input field (for example), plus an "edit" button:
This modification requires you to move the person's description from the server-side ViewModel to the client-side $scope. That means changing the controller code as well, since Razor does not need @Model.Description anymore, but you need to pass it (though JSON) to $scope.description:
This is not bad, and the code still stays readable and compact using the "pattern" I described a couple of posts ago. But I'd rather not touch the controller at all.
Using the couple of directives I wrote, it is as simple as:
or
No need for an additional $http request, nor for another controller action.
The "value" attibute on the input, or the element text of the span, will be written out by Razor as before; the new directives will use the content of the value attribute (or the element text) to initialize $scope.description. And without the need to do another round-trip to the server!
You don't even need to change the controller and split model fields between
Suppose you have a page to display some person details:
@model PersonVM
...
@Model.Name
@Model.Description
All is well, until you decide that you want to provide a way to edit the description. You do so by adding a input field (for example), plus an "edit" button:
{{description}}
This modification requires you to move the person's description from the server-side ViewModel to the client-side $scope. That means changing the controller code as well, since Razor does not need @Model.Description anymore, but you need to pass it (though JSON) to $scope.description:
$http.get("/Person/DetailsData" + $scope.personId).
success(function (data, status) {
$scope.description = data. description;
This is not bad, and the code still stays readable and compact using the "pattern" I described a couple of posts ago. But I'd rather not touch the controller at all.
Using the couple of directives I wrote, it is as simple as:
value='@Model.Description' ... >
{{description}}
or
ng-model="description" ng-content>@Model.Description
No need for an additional $http request, nor for another controller action.
The "value" attibute on the input, or the element text of the span, will be written out by Razor as before; the new directives will use the content of the value attribute (or the element text) to initialize $scope.description. And without the need to do another round-trip to the server!
You don't even need to change the controller and split model fields between
ViewResult Person::Details
and
JsonResult Person::DetailsData.