ng-value with 2 values and 2 ng-model in the same input-radio

Asked

Viewed 2,734 times

1

In the ion-radio has the ng-value with 2 values. The ng-model is taking the value of ng-value. The problem here is that I want to display these 2 values of ng-value separate.

ex: {{data.nomeOpc}} displays option name, and {{data.valorOpc}} displays the option value. In this model it displays the 2 together and I want to separate the values. I appreciate the help

     <ion-list>
        <h3>Opções</h3>
        <ion-radio ng-repeat = "child in produto.opcoes" value = "{{child}}" ng-change = "pegar(data)" ng-model = "data.opcoes">
           <strong>{{child.nomeOpc}}
              <span> = {{child.valorOpc | currency: 'R$'}}</span>
           </strong>
        </ion-radio>
     </ion-list>

Below is the function in the controller

$scope.pegar = function(data) {
 $scope.data = JSON.parse(data.opcoes);
  console.log($scope.data)
  console.log($scope.data.id, $scope.data.nomeOpc);
}

1 answer

0


Why don’t you pass the entire object in value and then manipulate it in the controller? It would be more or less like that in Angularjs:

var myApp = angular.module('myApp', []);

myApp.controller('MyCtrl', function($scope) {
    $scope.dados = [{
        id: 1,
        nome: 'Diego'
    }, {
        id: 2,
        nome: 'Fernando'
    }, {
        id: 3,
        nome: 'Bruno'
    }];
  
    $scope.pegar = function(data) {
        console.log(data)
        console.log(data.id, data.nome);
   
    }
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp" ng-controller="MyCtrl">
<ul>
     <li ng-repeat="item in dados">
            <label>{{item.nome}}
                <input type="radio" ng-model="data.nome" name="name" 
                value="{{item}}" ng-change="pegar(item)" />
            </label>
        </li>
    </ul>
</div>

NOTE: To view the result enter the browser developer mode and go to the console.


Updating:

In his ng-change instead of passing the content of ng-model pass the content of value:

<ion-radio ng-repeat = "child in dados" value = "{{child}}"  ng-model = "data.nome" ng-change = "pegar(child)"  ng-checked="true">

And then in your job pegar:

$scope.pegar = function(data) {
     //$scope.data = data;
     console.log(data)
     console.log(data.id, data.nome);
}

Example with Ionic: http://codepen.io/anon/pen/PNYOgK

  • opa...worked right here.. he returned as his example. Now how do I separate these values into $Scope separately. When I click on an option on the console it returns: Object {idOpc: "66", nameOpc: "01 person", valueOpc: "26.50"}

  • ex: $scope.item.idOpc, $scope.item.nomeOpc

  • I edited the example I posted, from a look.

  • guy...worked out tbm the example q vc passed me. They separated by Scope. More now from giving an error q I think beast, more do not know. When I switched to the last code you gave me, the radio checked symbol no longer appears. It takes the value no longer appears the checked symbol.

  • I updated the code according to what you went through. @Techies

  • @Alexandrescrideli I edited the answer and added an example working with Ionic

  • I set up a structure in the codepen, with the 2 examples you gave me. The last example solves the problem of the checked bug, but we can’t show which option set in a scope. Here follows the codepen link. http://codepen.io/alexandre_developer/pen/JXPwdd?editors=1011 @Techies

  • From to show in scope yes, just assign the date variable to a $Scope

  • @Alexandrescrideli I edited the answer, take a look at the codepen link

  • Note: In the code of the use ng-click button instead of ng-change

  • Brother sorry for the delay of the answer. Your code helped me a lot and I could understand a lot here, just reading your code.... thanks for the help anyway. @Techies

  • @Alexandrescrideli worked out?

  • Gave yes @Techies. With your code, I learned several things q did not know. You helped me pakas mine. Thanks even

  • I opened a new question here, if you can help me, I’d appreciate it very much myself. The link is http://answall.com/questions/116194/fun%C3%A7%C3%A3o-get-no-service-js-retorna-null? noredirect=1#comment242386_116194

  • Opa, good that it worked @Alexandrescrideli, continue programming and good luck. Don’t forget to mark the answer as correct so it helps other users with the same problem.

  • 1

    I know q is a mistake beast pakas, but I don’t know much programming. I’m more Dialing, so I’m learning little by little.

  • I’ll take a look at her

  • Thank you very much, brother...

Show 13 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.