-1

I have the following div:

<div ng-repeat="state in Home.overlaySettings.data.games[0].events[0].states">
    <div class="col-xs-12 col-md-4 noPadding" ng-repeat="feature in state.features" ng-show="state.name == Home.overlaySettings.data.games[0].events[0].state">
        <div class="form-group">
            <button 
                type="button" 
                class="btn largeButton" 
                ng-class="{'btn-success':feature.enabled == true,'btn-danger':feature.enabled == false}" 
                ng-click="feature.child('enabled').set(!feature.enabled);">
                    {{feature.name}}
            </button> 
        </div>
    </div>
</div>

The above code works with firebase. All I want to do is set the value from true to false when you click the button and then back again to true if you click it again.

My problem is that I can't figure out how to save the value. The feature is among many features and therefore I can't just search for it from the root element.

To show what Home.OverlaySettings is, have a look at this function:

function OverlaySettings(fbURL, $firebase, $q) {
    var fb = {"data":null,"ref":null,"ready":$q.defer()};
    initFB();
    function initFB() {
        fb.ref = new Firebase(fbURL);
        fb.data = $firebase(fb.ref).$asObject();
        fb.data.$loaded(  
            function(data) {
                fb.ready.resolve()
            }
        );
    }
    return function() {
        return fb;
    }
}

How can I save the value?

Bagzli
  • 6,254
  • 17
  • 80
  • 163
  • In this `Home.overlaySettings.ref.feature.child('enabled')` the `feature` is not defined. Remove it and you'll be setting a value in the database. Whether that is the value you want to set is hard to tell from what you shared. – Frank van Puffelen Jul 15 '17 at 03:21
  • @FrankvanPuffelen no it is not. I am trying to save the value of feature.enabled. The problem is that because I am looping features, I don't know which feature it is that I need to save. How do I get the full path of the current feature? – Bagzli Jul 15 '17 at 03:23
  • Ah. I think you're looking for `feature.$id`. So `Home.overlaySettings.ref.child(feature.$id).child('enabled').set(!feature.enabled);` See https://stackoverflow.com/questions/38045352/how-to-get-the-key-of-a-firebasearray-object, https://stackoverflow.com/questions/28997282/angularfire-firebase-geting-a-single-element-from-firebasearray-in-order-t, https://stackoverflow.com/questions/28862462/how-to-pull-key-name-from-array-of-objects-in-angularfire – Frank van Puffelen Jul 15 '17 at 03:38
  • @FrankvanPuffelen that gave me the following error: https://ibb.co/h7GCja – Bagzli Jul 15 '17 at 03:42
  • @FrankvanPuffelen I also tried printing it out with `{{feature.$id}}` and it just spat out blank. – Bagzli Jul 15 '17 at 03:48
  • You typed `feature.%id`. It should be `feature.$id`. – Frank van Puffelen Jul 15 '17 at 13:51
  • @FrankvanPuffelen I don't see where I entered % – Bagzli Jul 15 '17 at 15:12
  • The screenshot you posted https://ibb.co/h7GCja has `feature.%id` – Frank van Puffelen Jul 15 '17 at 15:54

1 Answers1

0

You can try like below.

Home.overlaySettings.data.games[0].events[0].states[$parent.$index].features[$index].child('enabled').set(!Home.overlaySettings.data.games[0].events[0].states[$parent.$index].features[$index].enabled);

Or you can do this job in your controller.

Mehmet Otkun
  • 1,374
  • 9
  • 22