I have controls that are model tied to ASP.net MVC5
@Html.TextBoxFor(model => model.OriginLocation.City, new { @class = "form-control", data_ng_model = "address1.City", test_change = "" })
So when the page loads the value of text box input is bound and should display value coming from service with Razor bound controls, latter i can manipulate that value which changes angular model for this control. What i have is textbox loads empty. I can see the value when I view source but its not displayed.
<input class="form-control ng-pristine ng-valid" data-ng-model="address1.City" data-val="true" data-val-length="The field City must be a string with a maximum length of 50." data-val-length-max="50" id="OriginLocation_City" name="OriginLocation.City" test-change="" type="text" value="Manheim">
js fragment
app.controller('LocationCtrl', ["$scope",
function ($scope) {
$scope.address1 = { Label: 'address1' };
Considering the answer by @Alan, I found a way that is "kind of" clean. Well that is the solution that I am using when creating ASP MVC Projects with Angular.js.
Instead of serializing the model inside the Angular controller (which would require to declare the controller inside of the Razor template), I serialize it in a global variable in each of my razor template like :
I also use the property attribute
[ScriptIgnore]
for the properties I don't want to serialize (that could cause circular references).After that, inside of my controllers, I simply do something like
Also, that way your angular Model always have the SAME properties has your ASP MVC Model.
One clean way to solve this life-cycle issue is by writing a directive for the "value" attribute for the input.
At first you can simply write the Razor's model value in yout input element by using the basic Razor syntax.
That will write the value of the @Model.Name in the input when the page is rendered, but this value will be replaced by empty values when Angular starts the bindings.
So you can write a directive, and on the "link" method you can play with the ngModel in order to keep the value that was present in the input before Angular cleans it. (The property 'prioriy: 0' indicates that the directive should be compiled as soon as possible)
That should restore the value written by Razor in your Angular's model
Angular will replace the contents of the text box with the value from its own model, which means you need to populate the Angular model. This can be achieved quickly by serializing your MVC model (or part of it) into your Angular model:
You can then render your MVC controls in the view like this:
ngModel
has precedence over the value that is originally set (it's setting the value to "" because the model doesn't exist). Take a look here...http://jsfiddle.net/yApeP/
But you can specify a value using
ngInit
...http://jsfiddle.net/D7vh7/
Which means you can use
ngInit
when generating the textbox...