angularjs – 与MVC Html.TextBoxFOr绑定的角度模型

前端之家收集整理的这篇文章主要介绍了angularjs – 与MVC Html.TextBoxFOr绑定的角度模型前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有与ASP.net MVC5绑定的模型控件

@Html.TextBoxFor(model => model.OriginLocation.City,new { @class = "form-control",data_ng_model = "address1.City",test_change = "" })

因此,当页面加载文本框输入的值被绑定并且应该显示来自具有Razor绑定控件的服务的值,后者我可以操纵该值来改变该控件的角度模型.
我所拥有的是文本框加载空.
我查看源时看到的值但是没有显示.

<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片段

app.controller('LocationCtrl',["$scope",function ($scope) {
  $scope.address1 = { Label: 'address1' };

解决方法

ngModel优先于最初设置的值(它将值设置为“”,因为模型不存在).看看这里……

http://jsfiddle.net/yApeP/

但您可以使用ngInit指定值…

http://jsfiddle.net/D7vh7/

这意味着您可以在生成文本框时使用ngInit …

@Html.TextBoxFor(model => model.OriginLocation.City,test_change = "",data_ng_init = string.Format("address1.City = '{0}'",Model.OriginLocation.City.Replace("'",@"\'"))  })

猜你在找的Angularjs相关文章