Clear the ng-model asssociated to textarea inside Angular Bootstrap UI tabset form an outside button

瘦欲@ 提交于 2019-12-11 02:50:45

问题


I have used angular bootstrap ui tabset to create two tabs and both of the tabs have textareas associated with a ng-model, i have a clear button outside the tabset and i want to clear the ng-model of the textArea in active tab when user presses the clear button. what is the best way to do this? this is what i have done so far.

HTML

<tabset>
    <tab heading="Tab One">
        <textarea data-ng-model="data.tabOne" class="form-control"></textarea>
    </tab>
    <tab heading="Tab two">
        <textarea data-ng-model="data.tabOne" class="form-control"></textarea>
    </tab>
</tabset>
<button ng-click="clearFn()" class="btn btn-default btn-float-left">Clear</button>

Controller

.controller('myController', ['$scope', function ($scope) {
        $scope.data = {
            tabOne: '',
            tabTwo: ''
        };

        $scope.ClearFn = function () {
            // I want to clear the model of the active tabs textArea here.
        };
    }]);

回答1:


You could use the tab's active attribute to find the current active tab.

<tabset>
  <tab heading="Tab One" active="activeState.tabOne">
    <textarea ng-model="data.tabOne" class="form-control"></textarea>
  </tab>
  <tab heading="Tab Two" active="activeState.tabTwo">
    <textarea ng-model="data.tabTwo" class="form-control"></textarea>
  </tab>
</tabset>

and in the controller:

.controller('myController', ['$scope', function ($scope) {
  $scope.data = {
    tabOne: 'ONE',
    tabTwo: 'TWO'
  };

  $scope.activeState = {};

  $scope.clearFn = function() {
    // I want to clear the model of the active tabs textArea here.
    for (var key in $scope.activeState) {
      if ($scope.activeState[key]) {
        // active tab found
        $scope.data[key] = '';
        return;
      }
    }
  };
}])

Example Plunker: http://plnkr.co/edit/ioJKua5XTeetBcvjGity?p=preview




回答2:


Since ng-model does a scope two way binding, in order to clear the ng-model, you can clear the scope variable.

.controller('myController', ['$scope', function ($scope) {
        $scope.data = {
            tabOne: '',
            tabTwo: ''
        };

        $scope.ClearFn = function () {
            // I want to clear the model of the active tabs textArea here.
             $scope.data.tabOne ='';
           };
    }]);


来源:https://stackoverflow.com/questions/25130595/clear-the-ng-model-asssociated-to-textarea-inside-angular-bootstrap-ui-tabset-fo

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!