简体   繁体   English

如何根据需要更改angularjs中的按钮标签?

[英]How to change button label in angularjs according to requirement?

I want to change the label of same button in angularjs according to requirement, same button can be for update and submit. 我想根据需要更改angularjs中相同按钮的标签,相同按钮可以用于更新和提交。

See the following demo , 参见以下演示

Above demo is about the replicate the template. 上面的演示是关于复制模板的。 If some template has already data then label of submit button should be Update, other-wise for empty template label should be Submit. 如果某个模板已经有数据,则“提交”按钮的标签应为“更新”,否则,为空模板的标签应为“提交”。

I can't use following logic because it will change all button with same label, but I want to show label Submit only for empty and no-empty to show label Update . 我不能使用以下逻辑,因为它将更改具有相同标签的所有按钮,但是我想显示标签“仅提交为空”和“不空的”以显示标签Update How I will do that? 我该怎么做?

<a class="btn btn-success" ng-click="updateOrder($index)">{{btnText}}</a> 
And add some logic to your controller, that will specify text for button:

if (newItem){
  $scope.btnText = 'Submit';
}else{
  $scope.btnText = 'Update';
} 

HTML 的HTML

 <div class="col-md-12" style="bottom:10px" >   
    <div class="form-group" ng-repeat="field in fields">  
        <div class="col-md-12">
          <div class="col-md-4"> 
              <label class="col-md-12 control-label">Field1</label>
              <div class="col-md-12">
                <input data-ng-model='field.field1' class="chosen-select input-md form-control sme-input-box"/>
              </div>
          </div>          
          <div class="col-md-4">          
            <label  class="col-md-12 control-label">Field2</label>
            <div class="col-md-12">            
                <input ng-model='field.field2'  class="chosen-select input-md form-control sme-input-box"/>
            </div>
          </div>
        </div>

        <div class="col-md-12">            
            <div class="col-md-3">
              <a class="btn btn-success" ng-click="removeTemplate($index)">Remove</a>   
            </div> 
            <div class="col-md-3">
              <a class="btn btn-success" ng-click="updateOrder($index)">Submit</a>   
            </div>                      
        </div>  
      </div>      
      <div class="col-md-3" style="top:5px">
              <a class="btn btn-success" ng-click="cloneTemplate()">Add</a>   
      </div>   
    </div>  

Angularjs AngularJS

  $scope.fields=[
            {
                "field1": "",
                "field2": "",
            }
        ]

        // update and get invoice details
        $scope.cloneTemplate=function(){
            var clone_template={ "field1": "", "field2": ""};
            $scope.fields.push(clone_template);
        }

        $scope.removeTemplate= function(templateIndex){
            $scope.fields.splice(templateIndex,1);
        }

        $scope.updateOrder=function(i){
            var updateOrder={
                "field1":$scope.fields[i].field1,
                "field2":$scope.fields[i].field2,
            }
            alert(updateOrder.field1);
            $http.post(config.server, updateOrder)
            .success(function(response, status){
                console.log(response);                
            })
            .error(function(response, status){
                console.log(response);
            })
        } 

I understand that while while adding data you wanted to show Submit button label & show Update button label while updating record. 我了解在添加数据时,您想在更新记录时显示“ Submit按钮标签并显示“ Update按钮标签。

So as normal practice you are getting this value from the DB, so I'd suggest you to add id column in this object which will be there with fields. 因此,通常的做法是从数据库中获取此值,因此建议您在此对象中添加id列,其中将包含字段。 Now object will look like {id: 1, field1: '1', field2: 2} so if the element has id that means it has persisted in the database. 现在对象看起来像{id: 1, field1: '1', field2: 2}因此,如果元素具有ID,则表示该元素已保留在数据库中。 And obviously if you don't have id in fields record means it has been added from UI. 显然,如果您在字段记录中没有id ,则意味着它已从UI中添加。

So the whole logic will look at the id property of your object, if you have id in the record then it will show Update as button label otherwise it would be Submit 因此,整个逻辑将查看对象的id属性,如果记录中包含id,则它将显示Update as button label,否则将显示Submit

<div class="col-md-3">
   <a class="btn btn-success" ng-click="updateOrder(field)" 
      ng-bind="field.id? 'Update': 'Submit'">
      Submit
   </a>   
</div>      

So for make your logic working good, you need to get the list again from the database to make your UI consistent. 因此,为了使您的逻辑运作良好,您需要再次从数据库中获取列表,以使UI保持一致。

Working Plunkr 工作朋克

Change your markup for button to show some scope property: 更改按钮的标记以显示一些范围属性:

<a class="btn btn-success" ng-click="updateOrder($index)">{{btnText}}</a> 

And add some logic to your controller, that will specify text for button: 并向您的控制器添加一些逻辑,该逻辑将为按钮指定文本:

if (newItem){
  $scope.btnText = 'Submit';
}else{
  $scope.btnText = 'Update';
}

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

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