AngularJs and ASP.NET MVC 5 - ng-model overriding textbox value

荒凉一梦 提交于 2019-12-05 06:44:21
Jasen

You can use ng-init to force a value from MVC

<input name="Town" type="text" ng-model="address.town" ng-init="address.town= @Model.Town" />

@Html.TextBoxFor(m => m.Town, new { ng_model="address.town", ng_init="address.town= Model.Town" })

Alternatively, I use a directive which I found here https://stackoverflow.com/a/22567485/2030565

app.directive('input', function ($parse) {
    return {
        restrict: 'E',
        require: '?ngModel',
        link: function (scope, element, attrs) {
            if (attrs.ngModel) {
                val = attrs.value || element.text();
                $parse(attrs.ngModel).assign(scope, val);
            }
        }
}; });

You could set a variable in a Javascript section at the bottom of the server template that your Angular controller assigns to the model on initialization.

<script>
     My.Namespace.town = @Html.Raw(Model.Town);
</script>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!