gpt4 book ai didi

javascript - angular 中的 ng-init 不起作用

转载 作者:行者123 更新时间:2023-11-30 09:36:19 25 4
gpt4 key购买 nike

我是 angularjs 的新手。尝试使用它来构建简单的应用程序,我在 Controller 中定义了一个名为 cuurent 的变量,现在我想像下面这样设置输入字段的值:

  <div class="form-group">
<input type="text" class="form-control" name="UserName" ng-model="management.regData.name" placeholder="UserName" ng-init="management.regData.name = '{{current.FullName}}'" required>
<p class="help-block" ng-show="(!editForm.UserName.$pristine && editForm.UserName.$error.required) || (editForm.$submitted && editForm.UserName.$error.required) ">this field is required</P>
<p class="help-block" ng-show="regUser.usernameMsg && !editForm.UserName.$error.required && editForm.UserName.$valid">{{ regUser.usernameMsg }}</p>
<ul ng-show="(!editForm.UserName.$pristine && editForm.UserName.$error.pattern) || (!editForm.UserName.$pristine && editForm.UserName.$error.minlength) || (!editForm.UserName.$pristine && editForm.UserName.$error.maxlength)" class="help-block">
<li>Must not contain any special characters or spaces</li>
<li>Must be at least 3 characters but no more than 25</li>
</ul>
</div>

问题是 ng-init="management.regData.name = '{{current.FullName}}'"不起作用。有什么建议 ?

最佳答案

去掉大括号,直接加上

ng-init="management.regData.name = current.FullName"

演示

angular.module("app",[])
.controller("ctrl",function($scope){
$scope.management={"regData":{"name":""}}
$scope.current = {"FullName":"sample"}
})
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app" ng-controller="ctrl">
<input type="text" class="form-control" name="UserName" ng-model="management.regData.name" placeholder="UserName" ng-init="management.regData.name = current.FullName" required>
</div>

关于javascript - angular 中的 ng-init 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43366208/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com