gpt4 book ai didi

javascript - AngularJS 无法设置文本区域的值

转载 作者:行者123 更新时间:2023-12-03 04:35:22 25 4
gpt4 key购买 nike

我试图为表单中的某些元素设置默认值,虽然它适用于所有输入字段,但它似乎不适用于文本区域,而代码几乎相同。

表单的 HTML:

<form class="form-horizontal" id="infoForm" name="form" ng-submit="ctrl.submitInfoForm(form)" novalidate>
<div class="form-group">
<label for="inputEditName">Name</label>
<input type="text" class="form-control" value="{{ctrl.viewedName}}" id="inputEditName" ng-model="viewUser.name">
</div>
<div class="form-group">
<label for="inputEditLocation">Location</label>
<input type="text" class="form-control" value="{{ctrl.viewedLocation}}" id="inputEditLocation" ng-model="viewUser.location">
</div>
<div class="form-group">
<label for="inputEditWebsite">Website</label>
<input type="text" class="form-control" value="{{ctrl.viewedWebsite}}" id="inputEditWebsite" ng-model="viewUser.website">
</div>
<div class="form-group">
<label for="inputEditBio">Bio</label>
<textarea rows="3" class="form-control" id="inputEditBio" ng-model="viewUser.bio" >{{ctrl.viewedBio}}</textarea>
</div>
<button type="submit" class="btn btn-default">Save Changes</button>
</form>

ProfileController.js 的相关部分:

var vm = this;
vm.getViewedName = getViewedName;
vm.getViewedLocation = getViewedLocation;
vm.getViewedWebsite = getViewedWebsite;
vm.getViewedBio = getViewedBio;
vm.viewedName = getViewedName();
vm.viewedLocation = getViewedLocation();
vm.viewedWebsite = getViewedWebsite();
vm.viewedBio = getViewedBio();

function getViewedName() {
if (vm.viewedName === undefined) {
var viewedName;
var viewedUser = UserService2.getViewedUser();
viewedName = viewedUser.name;
if (viewedName === "") {
viewedName = "-";
}
return viewedName;
} else {
return vm.viewedName;
}
}

function getViewedLocation() {
if (vm.viewedLocation === undefined) {
var viewedLocation;
var viewedUser = UserService2.getViewedUser();
viewedLocation = viewedUser.location;
if (viewedLocation === "") {
viewedLocation = "-";
}
return viewedLocation;
} else {
return vm.viewedLocation;
}
}

function getViewedWebsite() {
if (vm.viewedWebsite === undefined) {
var viewedWebsite;
var viewedUser = UserService2.getViewedUser();
viewedWebsite = viewedUser.website;
if (viewedWebsite === "") {
viewedWebsite = "-";
}
return viewedWebsite;
} else {
return vm.viewedWebsite;
}
}

function getViewedBio() {
if (vm.viewedBio === undefined) {
var viewedBio;
var viewedUser = UserService2.getViewedUser();
viewedBio = viewedUser.bio;
if (viewedBio === "") {
viewedBio = "-";
}
return viewedBio;
} else {
return vm.viewedBio;
}
}

默认情况下仅设置名称,因此此时它看起来像这样:

姓名:我的名字地点: -网站: -简介:

最佳答案

我发现我做错了什么

首先,我有一个 ngModel 和附加到文本区域的 Controller 字段(以及其他输入),所以我删除了该字段,现在我只使用 ngModel。

其次,我忘了按 ctrl。在我的 ngModel 属性中。我通过简单地添加 ctrl 来改变这一点。到我的 ngModel 属性。

所以我已经改变了一切,它现在可以工作了,看起来像这样:

HTML:

<form class="form-horizontal" id="infoForm" name="form" ng-submit="ctrl.submitInfoForm(form)" novalidate>
<div class="form-group">
<label for="inputEditName">Name</label>
<input type="text" class="form-control" id="inputEditName" ng-model="ctrl.viewUser.name">
</div>
<div class="form-group">
<label for="inputEditLocation">Location</label>
<input type="text" class="form-control" id="inputEditLocation" ng-model="ctrl.viewUser.location">
</div>
<div class="form-group">
<label for="inputEditWebsite">Website</label>
<input type="text" class="form-control" id="inputEditWebsite" ng-model="ctrl.viewUser.website">
</div>
<div class="form-group">
<label for="inputEditBio">Bio</label>
<textarea rows="3" class="form-control" id="inputEditBio" ng-model="ctrl.viewUser.bio" ></textarea>
</div>
<button type="submit" class="btn btn-default">Save Changes</button>
</form>

ProfileController.js:

var vm = this;
vm.getViewedUser = getViewedUser;
vm.viewUser = getViewedUser();


function getViewedUser() {
if (vm.viewUser === undefined) {
vm.viewUser = UserService2.getViewedUser();
if (vm.viewUser.name === "") {
vm.viewUser.name = "-";
}
if (vm.viewUser.location === "") {
vm.viewUser.location = "-";
}
if (vm.viewUser.website === "") {
vm.viewUser.website = "-";
}
if (vm.viewUser.bio === "") {
vm.viewUser.bio = "-";
}
return vm.viewUser;
} else {
return vm.viewUser;
}
}

Founded1898的答案以及 Vinod Louis 的评论有帮助,我觉得那些并没有完全回答我的问题,这就是我添加这个答案的原因。

关于javascript - AngularJS 无法设置文本区域的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43322793/

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