angularjs - Angular 模型与 MVC Html.TextBoxFOr 的绑定(bind)

标签 angularjs asp.net-mvc-5

我有模型绑定(bind)到 ASP.net MVC5 的控件

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

因此,当页面加载文本框输入的值时,文本框输入的值是绑定(bind)的,并且应该显示来自带有 Razor 绑定(bind)控件的服务的值,后者我可以操纵改变该控件的 Angular 模型的值。
我所拥有的是文本框加载为空。
查看源代码时可以看到该值,但未显示。
<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,
    new { @class = "form-control", 
         data_ng_model = "address1.City", 
         test_change = "",
         data_ng_init = string.Format("address1.City = '{0}'", Model.OriginLocation.City.Replace("'", @"\'"))  })

关于angularjs - Angular 模型与 MVC Html.TextBoxFOr 的绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24858215/

相关文章:

javascript - 带有 Angular JS ng-view 的 Phonegap 无法全屏显示

javascript - 使用纯 javascript 拖放并再次拖动

c# - 解决 Razor 重大变化的最佳方法

javascript - 错误: [$injector:nomod] when attempting to add 3rd party module to mean app.

node.js - 为什么 npm 默认在本地安装模块?

angularjs - 如何模拟 $window 来单元测试 AngularJS 服务?

c# - 如何导出到 Excel?

javascript - MVC - 将整个模型作为参数传递给 javascript 中的 Url.Action

asp.net - 如何在自定义错误页面中访问 HTTP StatusDescription

asp.net-mvc - 安装 Oracle Developer Tools for Visual Studio 2017 时出错