How to control ng-repeat divs from ng-repeat inputs

So, just getting started in Angular and it's pretty tricky, coming from a pretty simple JS and jQuery background. Here's what I'm trying to do. I have a "tag template" that has a couple categories and then some sub-tags contained within. I have defined these as an object, with the idea that the object/file can be called via file request and manipulated, etc.

I have loaded labels and tag category inputs dynamically by using a factory service and a controller with ng-repeat. Likewise, I have deposited the subtags into another div on page2 (using jQuery mobile page swiping). I'd like to use the checkbox state of the category tags to show/hide the sub-tags on page2.

I have tried dozens of things and searched all over stackexchange, the net, etc, but is simple and straightforward and similar enough for me to get it working. If someone can point me in the right direction, that would be great. Keep in mind that my next step is to add a button on page 1 to add a new category, and buttons on page 2 to add sub-tags to the sub-tag categories.

Finally, I have one more weird thing to report. If I only have two pages in my DOM, I have some weird behavior when loading the page. If I load from page 1, the tag checkboxes do not function, and I see a slight fattening of the border of the labels. If I swipe left to page 2 and reload from this page, the borders of the labels are thin and the checkboxes function. Cannot track down why this would be happening. My hacky workaround is to add an empty page zero and then changepage immediately to page one, but this is far from ideal. Any thoughts on that would be appreciated as well.

Here it is:

HTML

   <!-- Angular version    -->

            <button class="ui-btn" onclick="selectTemplate();">My Template</button> 
            <form>
                <div data-role="controlgroup">
                    <fieldset data-role="controlgroup"> 
                       <div ng-controller="templateCtrl">
                            <label 
                            class="ui-checkbox" 
                            ng-style="{backgroundColor: '{{tagCat.color | bgColor}}'}" 
                            ng-repeat="tagCat in template"><input type="checkbox" 
                            class="ui-checkbox" 
                            id="{{tagCat.name}}" 
                            ng-model="clicked"
                            ng-click="click();" 
                            />{{tagCat.name}}</label>
                            <div ng-repeat="tagCat in template">{{cb}} {{tagCat.name}} hallo</div>
                        </div>

                    </fieldset>
                </div>
                <div style="display:none" class="flashNotification"></div>        
            </form>                 
    </div>

    <div data-role="page" id="two">
        <button class="ui-btn" onclick="selectTemplate();">My Template</button> 
            <form>
                <div data-role="controlgroup">
                    <div ng-controller="templateCtrl">
                        <div ng-repeat="tagCat in template" ng-show="clicked" class="{{tagCat.name}}">{{tagCat.name}}
                            <fieldset data-role="controlgroup">
                                <label class="ui-checkbox" 
                                ng-repeat="item in tagCat.items" 
                                ng-style="{backgroundColor: '{{tagCat.color | bgColor}}'}" 
                                for="item.name">{{tagCat.color | bgColor}}
                                <input class="ui-checkbox" 
                                name="{{item.name}}" 
                                id='{{item.name}}' 
                                type="checkbox" />{{item.name}}</label>
                            </fieldset>
                        </div>
                    </div>                  
                </div>
                <div style="display:none" class="flashNotification"></div>        
            </form>
    </div>
</div>

JS for jQuery Mobile

$(document).ready(function() {
  // addTemplateItems(tagTemplate); // not necessary with Angular
  // $.mobile.changePage('#two', { transition: 'none' });  // required or checkboxes don't work on load
  $.mobile.changePage('#one', { transition: 'none' }); 
//   // $("[data-role=controlgroup]").controlgroup("refresh");

  // set up page nav
  $(document).delegate('.ui-page', "swipeleft", function(){
      var $nextPage = $(this).next('[data-role="page"]');
      var $prevPage = $(this).prev('[data-role="page"]');

      console.log("binding to swipe-left on "+$(this).attr('id') );
      // swipe using id of next page if exists
      if ($nextPage.length > 0) {
          $.mobile.changePage($nextPage, { transition: 'slide' });
      } else {
          var message = 'tagged!';

          // save tags here

          flashNotify(message);
          console.log('fire event!');
          $('#flashNotification').promise().done(function () {
            $('#group1').hide();
            $('#group2').hide();
            $('.ui-btn').hide();
            // addTemplateItems(tagTemplate);
            $.mobile.changePage($prevPage, { transition: 'none' });
            captureImage();
          }); 
      }
  }).delegate('.ui-page', "swiperight", function(){
      var $prevPage = $(this).prev('[data-role="page"]');
      console.log("binding to swipe-right on "+$(this).attr('id') );
      // swipe using id of next page if exists
      if ($prevPage .length > 0) {
          $.mobile.changePage($prevPage, { transition: 'slide', reverse : true });
      } else {
          alert('no backy backy!');
      }
  });

// $("input[type='checkbox']").checkboxradio().checkboxradio("refresh");

});

JS for Angular App

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

app.factory('TagTemplate', [function () {

  var TagTemplate = {};

  var tagTemplate = {
    family: {
      name: "family",
      description: "These are your family members.",
      color: "red",
      items: [
        {
          name: "Joe"
        },
        {      
          name: "Mary"
        },
        {
          name: "Jim"
        }
      ]
    },
    design: {
      name: "design",
      description: "Different types of design notes.",
      color: "blue",
      items: [
        {
          name: "inspiring"
        },
        {
          name: "fail"
        },
        {
          name: "wayfinding"
        },
        {
          name: "graphics"
        }
      ]
    },
    work: {
      name: "work",
      description: "Stuff for work.",
      color: "green",
      items: [
        {
          name: "whiteboard"
        },
        {
          name: "meeting"
        },      
        {
          name: "event"
        }
      ]
    }
  };

  TagTemplate = tagTemplate;

  return TagTemplate;
}])



// Controller that passes the app factory

function templateCtrl($scope, TagTemplate) {
  $scope.template = TagTemplate;

  $scope.click = function(model) {
    console.log(this.checked, this.tagCat.name);
  }


}


app.filter('bgColor', function () {
  return function (color) {
    // console.log(color, $.Color(color).lightness(.05).toHexString(.05));
    // var rgba = $.Color(color).alpha(.05);

    return $.Color(color).lightness(.97).toHexString();
  }
})

Answers


For the main part, success!

I found a jsfiddle that gave me a good base for experimenting. After some playing, I realized that I just have to create a show property within each of the categories in my data service model, and then assign the ng-model to that property to control it.

I had to do it slightly differently in my own code, but the understanding gained from the following jsfiddle led me to the answer:

http://jsfiddle.net/Y43yP/

HTML

<div ng-app ng-controller="Ctrl">
    <div class="control-group" ng-repeat="field in customFields">
        <label class="control-label">{{field}}</label>
        <div class="controls">
            <input type="text" ng-model="person.customfields[field]" />
            <label><input type="checkbox" ng-model="person.show[field]" /></label>
        </div>
    </div>

    <button ng-click="collectData()">Collect</button><button ng-click="addField()">Add Field</button><br/><br/>

    <em>Booleans</em>
    <div ng-repeat="field in customFields">
        <p>{{field}}: {{person.show[field]}}</p>
    </div>
    <em>Show/Hide</em>
    <div ng-repeat="field in customFields">
        <p ng-show="person.show[field]">{{field}}: {{person.customfields[field]}}</p>
    </div>
</div>

JS

function Ctrl($scope) {
    $scope.customFields = ["Age", "Weight", "Height"];
    $scope.person = {
        customfields: {
             "Age": 0,
             "Weight": 0,
             "Height": 0
        },
         show: {
             "Age": false,
             "Weight": false,
             "Height": false
        }
    };

    $scope.collectData = function () {
        console.log($scope.person.customfields, $scope.person.show);
    }

    $scope.addField = function () {
        var newField = prompt('Name your field');
        $scope.customFields.push(newField);

    }
}

Still having the checkbox issue but I'll open a separate issue for that if I can't figure it out.

Thanks.


Need Your Help

How to animate to other position that center map

android animation controller point mapactivity

I have an activity that extends MapActivity, and inside onCreate() I have this code

MySQL statement, JOIN

mysql

I have these tables :