问题
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