How to change button label in angularjs according to requirement?

前端 未结 2 1922
轮回少年
轮回少年 2021-01-29 01:28

I want to change the label of same button in angularjs according to requirement, same button can be for update and submit.

See the following demo,

Above demo is

相关标签:
2条回答
  • 2021-01-29 02:22

    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';
    }
    
    0 讨论(0)
  • 2021-01-29 02:27

    I understand that while while adding data you wanted to show Submit button label & show Update button label while updating record.

    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. 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. And obviously if you don't have id in fields record means it has been added from 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

    <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.

    Working Plunkr

    0 讨论(0)
提交回复
热议问题