简体   繁体   English

根据HTML输入(角度)更改对象属性

[英]Change object attributes based on HTML input (angular)

I don't know how to proceed with my problem, so that's why i'm hopping you guys could help. 我不知道如何解决我的问题,所以这就是为什么我希望你们能提供帮助。 I have the HTML form which looks like this: 我有看起来像这样的HTML表单:

<form>
<div class="form-group">
        <label for="Uid" class="col-sm-1 control-label">Uid</label>
        <div class="col-sm-4">
            <input type="text" class="form-control" ng-model="subscriber.subscriberUid" >
        </div>
    </div>
    <div class="form-group">
        <label for="name" class="col-sm-1 control-label">Name</label>
        <div class="col-sm-4">
            <input type="text" class="form-control" ng-model="subscriber.name" >
        </div>
    </div>
    <div class="form-group">
        <label for="surname" class="col-sm-1 control-label">Surename</label>
        <div class="col-sm-4">
            <input type="text" class="form-control" data-ng-model="subscriber.surname">
        </div>
    </div>
</form>

And this is my angular app, where i store subscriber data, so i can insert it: 这是我的角度应用程序,用于存储订户数据,因此我可以将其插入:

//subscriber
$scope.subscriber = {
     "subscriberUid" : ""
     "name": "",
     "surname": "",
};

So where is the tricky part for me. 那么,对我来说棘手的部分在哪里。
If user inputs the subscriberUid then my subscriber object shoul look like this: 如果用户输入subscriberUid,则我的订户对象应如下所示:

 //subscriber
    $scope.subscriber = {
         "subscriberUid" : ""
         "name": "",
         "surname": "",
    };

Otherwise it needs to look like this and back-end will auto generate it: 否则它需要看起来像这样,后端将自动生成它:

 //subscriber
    $scope.subscriber = {
         "name": "",
         "surname": "",
    };

How to achieve desired effect? 如何达到理想的效果?

Thank you in advance! 先感谢您!

You can check before sending to backend if $subscriber.subscriberUid is empty and just remove it from object with 您可以在发送到后端之前检查$ subscriber.subscriberUid是否为空,并使用以下命令将其从对象中删除

        if($scope.subscriber.subscriberUid == ""){
            delete $scope.subscriber.subscriberUid;
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM