-1

This is my punker

I can get the selected values pushed to array by ngcheck and ngchange, but I want to push the {{get_ing_value.productName}} to array when I click any of the checkbox and radio button.

This is what I have:

[
    {
        "selected": {
            "_id": "584aa4e218c5b62c02b6fe6a",
            "productId": "584aa4e118c5b62c02b6fe67",
            "ingredientTypeId": "5822f17b24baa627770af5ed",
            "ingredientName": "small",
            "slug": "small",
            "isActive": true,
            "ingredientPrice": -75
        }
    },
    {
        "selected": {
            "_id": "584aa4e718c5b62c02b6fe6e",
            "productId": "584aa4e118c5b62c02b6fe67",
            "ingredientTypeId": "5822f18324baa627770af5ee",
            "ingredientName": "extra cheese",
            "slug": "extra-cheese",
            "isActive": true,
            "ingredientPrice": 20
        }
    },
    {
        "selected": "{\"_id\":\"584aa4e918c5b62c02b6fe70\",\"productId\":\"584aa4e118c5b62c02b6fe67\",\"ingredientTypeId\":\"5822f19724baa627770af5ef\",\"ingredientName\":\"hot chilly\",\"slug\":\"hot-chilly\",\"isActive\":true,\"ingredientPrice\":20}"
    },
    {
        "selected": "{\"_id\":\"584aa4ea18c5b62c02b6fe71\",\"productId\":\"584aa4e118c5b62c02b6fe67\",\"ingredientTypeId\":\"5822f1a024baa627770af5f0\",\"ingredientName\":\"chicken\",\"slug\":\"chicken\",\"isActive\":true,\"ingredientPrice\":50}"
    }
]

This is what I want to get

[
    {
        "main_product_name": "PIZAA"
    },
    {
        "selected": {
            "_id": "584aa4e218c5b62c02b6fe6a",
            "productId": "584aa4e118c5b62c02b6fe67",
            "ingredientTypeId": "5822f17b24baa627770af5ed",
            "ingredientName": "small",
            "slug": "small",
            "isActive": true,
            "ingredientPrice": -75
        }
    },
    {
        "selected": {
            "_id": "584aa4e718c5b62c02b6fe6e",
            "productId": "584aa4e118c5b62c02b6fe67",
            "ingredientTypeId": "5822f18324baa627770af5ee",
            "ingredientName": "extra cheese",
            "slug": "extra-cheese",
            "isActive": true,
            "ingredientPrice": 20
        }
    },
    {
        "selected": "{\"_id\":\"584aa4e918c5b62c02b6fe70\",\"productId\":\"584aa4e118c5b62c02b6fe67\",\"ingredientTypeId\":\"5822f19724baa627770af5ef\",\"ingredientName\":\"hot chilly\",\"slug\":\"hot-chilly\",\"isActive\":true,\"ingredientPrice\":20}"
    },
    {
        "selected": "{\"_id\":\"584aa4ea18c5b62c02b6fe71\",\"productId\":\"584aa4e118c5b62c02b6fe67\",\"ingredientTypeId\":\"5822f1a024baa627770af5f0\",\"ingredientName\":\"chicken\",\"slug\":\"chicken\",\"isActive\":true,\"ingredientPrice\":50}"
    }
]
4
  • Plnkrs are fine for augmenting your question, but you'll still have to put a substantial relevant part of your code into your actual question here for it to make sense. Commented Dec 19, 2016 at 10:18
  • okay thank you for the edits Commented Dec 19, 2016 at 10:24
  • You mean something like this? Commented Dec 19, 2016 at 10:28
  • no if i click get selected checkbox and radio values button the only selected values are of pushing into array instead of that i want to push that main name PIZZA to array Commented Dec 19, 2016 at 10:33

1 Answer 1

0

You can try this code, may be it helps you. It will set PIZZA as main_product_name in first index of your array when you select any radio or checkbox, it will also check if main_product_name is already added.

var App = angular.module('App', []);
App.controller('TodoCtrl', function($scope, $http) {

   $scope.get_product_ingredients;
   $http.get('https://api.myjson.com/bins/1gh1pl').then(function(res) {
    $scope.get_product_ingredients = res.data;
   });
  $scope.selected_ingrediants = [];
  $scope.tempCheck = {};
  $scope.formatted = [];

  $scope.get = function() {
    if(!$scope.formatted[0]){
     $scope.formatted[0] = {
      main_product_name: $scope.get_product_ingredients[0].productName
     }
   }
  };

 $scope.updateIngredient = function(type, model, index){
   if(!$scope.formatted[0]){
     $scope.formatted[0] = {
      main_product_name: $scope.get_product_ingredients[0].productName
     }
   }
   $scope.formatted[index+1] = {
     //ingredientTypeName: type,
    selected: model
   }

  }

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

6 Comments

Thank you but this is not working in my plunker can you post plunker
exactly but if i click two values in NON-VEGETABLES means one value get disappeared
check now, updated your HTML this time :) plnkr.co/edit/PfV53gicxwHLO6oG9SsK?p=preview
Thank you so much can you explain
Yes, 1. Actually you were sending a the key to the function updateIngredient(), where you used that key as index. The value of key was starting from 0 and maximum to the length of array. I just added [index+1] in this function, so that it will always take index from 1 to maximumLength+1, it means 0 index is free always. On 0 index i check if there is value or not, if not then put main_product_name there.
|

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.