0

I'm encountering a problem and I will appreciate someone can help.

Right now I'm showing several checkboxes in my form which are grabbing from my firebase database. And in my controller I'm trying to use ng-submit to store whole data of form. My checkbox JSON format should look like this.

{
    "apple" : true,
    "spotify" : true 
}

The checkbox image here

okay the following is my code from HTML and Javascript

<label class="checkbox-inline" ng-repeat="hashtag in hashtags" for="{{hashtag}}">
  <input type="checkbox" name="{{hashtag.$value}}" id="{{hashtag.$value}}" value="{{hashtag.$value}}" ng-model="hashtag.SELECTED" ng-true-value="true" ng-false-value="false"> 
    {{hashtag.$value | capitalize}}
</label>

My Javascript here

form.$add({
    description: $scope.inputDescription,
    hashtags: {
        *I don't know how to put checkbox's result here.*
    },
    time: Firebase.ServerValue.TIMESTAMP,
    title: $scope.inputTitle,
    url: $scope.inputUrl
})

Anyone can help, please? Thank you so much.

Update Thanks for the reply. I really appreciate those! However, I found out that I forgot to mention something and I tried the answers from the reply and they didn't work. The picture below will explain my problem. enter image description here Thank you so much!

Finally solution in case people are curious about the answer I just figured out. The following is my solution. Thank you guys! I love stackoverflow!

function checkbox(hashtags) {
    var arr = {};
    for(var hashtag in hashtags) {
        if(hashtags[hashtag].SELECTED === true){
            arr[hashtags[hashtag].$value] = true;
        }
    }
    return arr;
}
1
  • is hashtags the first described JSON? Commented Oct 6, 2015 at 18:53

3 Answers 3

1

Something like this, I found info in a similar question:

<input type="submit" name="submit" value="submit" ng-click="check(hashtags)"/>

$scope.check= function(data) { 
var arr = [];
for(var i in data){
   if(data[i].SELECTED=='Y'){
       arr.push(data[i].value);
   }
}
//At this points you have all the selected hashtags on arr Array

}

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

1 Comment

Thank you so much Chris! I did it. At first I used you answer but somehow just didn't work. And I used this answer again and twitch a little bit and it works this time! Thanks you!
0

Try changing your ng-model to

ng-model="mhashtags[hashtag.$value]"

With this you will have a mhashtags variable in your scope that will be exactly what you need to send.

Comments

0

Assuming that hashtags is this JSON:

$scope.hashtags = {
  "apple" : true,
  "spotify" : true 
};

I would change the ng-repeat to this:

<label class="checkbox-inline" ng-repeat="(hashtag, bool) in hashtags" for="{{hashtag}}">
  <input type="checkbox" name="{{hashtag}}" id="{{hashtag}}" ng-value="bool" ng-model="hashtag"> 
    {{hashtag | capitalize}}
</label>

hashtag is the key of the object and bool is the value, so no need for "$value".

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.