1

I'm pretty new to AngularJs and I'm locked on a little part of code:

In my UI, I've a radio button and a table (ng-grid). In my controller, I've a json array for each radio choices.

How Can I dynamically bind my table datas dependending on the selected radio button?

Example: I've a radio with A and B choices.

 <div class="btn-group">
            <button type="button" class="btn btn-primary" ng-model="choice" btn-radio="'A'">A</button>
            <button type="button" class="btn btn-primary" ng-model="choice" btn-radio="'B'">B</button>
        </div>
<div class="gridStyle" ng-grid="gridOptions">
        </div>

Controllers got three arrays:

$scope.choice = 'A'
$scope.A = {name: 'A', propA: 'Aprop'}
$scope.B = {name: 'B', propB: 'Bprop'}
$scope.gridOptions = ?????

Thank you in advance.

Charlie

2
  • One quick question: what is btn-radio in your radio element? Is it a subsitute for value? edit: I guess it's a bootstrap button radio equivalent. Commented Jun 24, 2013 at 8:15
  • Angular-bootstrap (angular-ui) component for radio button. The btnradio directive will switch the $scope.choice value in the controller. Commented Jun 24, 2013 at 8:16

2 Answers 2

2

You can watch $scope.choice:

$scope.$watch('choice', function(newVal, oldVal) {
  $scope.gridOptions = $scope.choice;
});

This requires that you set $scope.choice to the whole $scope.A object. I don't know if this works with bootstrap radio buttons, but it works with normal angular radio buttons.

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

1 Comment

Yes, $watch seems good. I think I'll read the angular documentation next time. Thank you!
2

You can set a scope.$watch expression on "choice" that swaps out the data being assigned to gridOptions.

Inside your controller

$scope.$watch('choice', function(newValue, oldValue) {
    if ($scope.choice == 'A') { 
        $scope.gridOptions = ?????? 
    } else { 
        $scope.gridOptions = ????? 
    }
})

EDIT: $digest is not necessary, as the watch expression is only called during a digest cycle.

1 Comment

It shouldn't be necessary to call $scope.digest(), because $watch triggers a digest cycle anyway.

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.