【问题标题】:How to control ng-repeat divs from ng-repeat inputs如何从 ng-repeat 输入控制 ng-repeat div
【发布时间】:2014-03-15 12:54:19
【问题描述】:

所以,刚刚开始使用 Angular,它非常棘手,来自非常简单的 JS 和 jQuery 背景。这就是我想要做的。我有一个“标签模板”,它有几个类别,然后包含一些子标签。我已经将它们定义为一个对象,其想法是可以通过文件请求和操作等方式调用对象/文件。

我使用工厂服务和带有 ng-repeat 的控制器动态加载了标签和标签类别输入。同样,我已将子标签存放到 page2 上的另一个 div 中(使用 jQuery 移动页面滑动)。我想使用类别标签的复选框状态来显示/隐藏 page2 上的子标签。

我已经尝试了很多东西,并在 stackexchange、网络等平台上进行了搜索,但它简单、直接且相似,足以让我让它工作。如果有人能指出我正确的方向,那就太好了。请记住,我的下一步是在第 1 页添加一个按钮以添加新类别,并在第 2 页添加按钮以将子标签添加到子标签类别。

最后,我还要报告一件奇怪的事情。如果我的 DOM 中只有两个页面,那么在加载页面时会出现一些奇怪的行为。如果我从第 1 页加载,标签复选框不起作用,并且我看到标签的边框略微变胖。如果我向左滑动到第 2 页并从此页面重新加载,则标签的边框很薄并且复选框起作用。无法追查为什么会发生这种情况。我的 hacky 解决方法是添加一个空页面零,然后立即将页面更改为第一页,但这远非理想。对此的任何想法也将不胜感激。

这里是:

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>

用于 jQuery Mobile 的 JS

$(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");

});

Angular 应用的 JS

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();
  }
})

【问题讨论】:

    标签: html angularjs jquery-mobile dynamic input


    【解决方案1】:

    对于主要部分,成功!

    我找到了一个 jsfiddle,它为我提供了良好的实验基础。玩了一段时间后,我意识到我只需要在我的数据服务模型的每个类别中创建一个 show 属性,然后将 ng-model 分配给该属性来控制它。

    我不得不在自己的代码中稍作不同,但从以下 jsfiddle 中获得的理解使我得到了答案:

    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);
    
        }
    }
    

    仍然有复选框问题,但如果我无法解决,我会为此打开一个单独的问题。

    谢谢。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-11-23
      • 1970-01-01
      • 2017-12-22
      • 2013-04-05
      • 1970-01-01
      • 2012-11-22
      • 2017-07-27
      相关资源
      最近更新 更多