AngularJS无法设置值textarea

时间:2017-04-10 12:09:57

标签: javascript html angularjs textarea

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

表单的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;
    }
}

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

姓名:myname 地点: - 网站: - 生物:

2 个答案:

答案 0 :(得分:1)

来自Docs

  

ngModel将尝试通过评估当前范围上的表达式绑定到给定的属性。如果该范围上尚不存在该属性,则将隐式创建该属性并将其添加到范围中。

因此,在你的Case中,angular正试图将Textarea绑定到viewUser.bio属性。由于它不存在,它将使用空值创建它,然后“覆盖”您在html中提供的ctrl.viewedBio

解决问题的简单方法:

在您的Controller中(在Init函数或类似的东西中......)

vm.viewUser.bio = vm.viewedBio

答案 1 :(得分:0)

我发现了我做错了什么。

首先,我有一个ngModel和一个控制器的字段附加到textarea(以及其他输入),所以我删除了字段,现在我只是使用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的评论有所帮助,但我觉得这些并没有完全回答我的问题,而这就是我添加这个答案的原因。