1

I have a button group and I want to pass the value of the selected button back to my controller but it isn't working, it just returns undefined...

HTML

<body ng-app="myApp">
    <div ng-controller="myCtrl">
        <button type="button" ng-model="activeCustomer" value="active" ng-click="getVal()" class="btn btn-default">Active</button>
        <button type="button" ng-model="activeCustomer" value="inactive" ng-click="getVal()" class="btn btn-default">Inactive</button>
        <button type="button" ng-model="activeCustomer" value="all" ng-click="getVal()" class="btn btn-default">All</button>

        {{change}}
    </div>

Controller :

aap=angular.module('myApp',[])
.controller('myCtrl',["$scope",function($scope){
  //set the radio buttons
    $scope.change='the data';
    $scope.getVal=function(){
        console.log($scope.change);
        console.log($scope.activeCustomer);
        $scope.change=$scope.activeCustomer;
    }
}]);

however if I change the code from <button type="button" to <input type="radio" it works! Any ideas? Thanks

http://plnkr.co/edit/Hfu7EGQ05hA59Sgty29H?p=preview

3 Answers 3

2

here is the working code -

<body ng-app="myApp">
<div ng-controller="myCtrl">
        <button type="button" ng-model="activeCustomer" value="active" ng-click="getVal($event)" class="btn btn-default">Active</button>
        <button type="button" ng-model="activeCustomer" value="inactive" ng-click="getVal($event)" class="btn btn-default">Inactive</button>
        <button type="button" ng-model="activeCustomer" value="all" ng-click="getVal($event)" class="btn btn-default">All</button>

        {{change}}
    </div>


<script>
aap=angular.module('myApp',[])
.controller('myCtrl',["$scope",function($scope){
  //set the radio buttons
    $scope.change='the data';
    $scope.getVal=function(active){
        console.log($scope.change);
        console.log(active.currentTarget.value);
        $scope.change=active.currentTarget.value;
    }
}]);


</script>

You need to pass the $event for each button and access it in controller. Then after you can change the value.

Here is the plunker:-

http://plnkr.co/edit/wkzJ46zRoczkMPomzEok?p=preview

Sign up to request clarification or add additional context in comments.

1 Comment

always welcome.. glad to know that my answer solved your problem.. :) :)
2

HTML

 <div ng-controller="myCtrl">
            <button type="button"
                    value="active"
                    ng-click="getVal('active')"
                    class="btn btn-default">
                Active
            </button>
            <button type="button"
                    value="inactive" 
                    ng-click="getVal('inactive')"
                    class="btn btn-default">
                Inactive
            </button>
            <button type="button"
                    value="all" 
                    ng-click="getVal('all')"
                    class="btn btn-default">
                 All
            </button>
 </div>

Controller :

.controller('myCtrl',["$scope",function($scope){
    $scope.getVal=function(buttonClicked){
        // buttonClicked contains the button name which is clicked
        console.log(buttonClicked);
    }
}]);

Comments

1

Remember that Bootstrap is just a CSS library that makes things look pretty; it doesn't change the behavior of any browser elements. By default, HTML <button> tags aren't meant to be form elements that represent data, so ng-model doesn't know how to attach to them. If you want a radio button group to work with ng-model, you'll need to use the radio button directive in UI Bootstrap, or some other similar directive.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.