首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在JQuery中从角调用ng-attr-id

在JQuery中从角调用ng-attr-id
EN

Stack Overflow用户
提问于 2017-01-13 17:10:54
回答 1查看 830关注 0票数 0

下面的代码集成到我的html页面中:

代码语言:javascript
复制
<div ng-controller="MirrorCheckboxController">

  <div ng-repeat="setting in settings">
    <input type="checkbox" ng-attr-id="{{ setting.id }}">
    <label ng-attr-for="{{ setting.id }}"><span class="checkbox"{{setting.name}}</span></label>
  </div>

</div>



<div id="events">

  <input type="checkbox" id="green">
  <label for="green"><span>Sports</span></label> <br/>

  <input type="checkbox" id="red">
  <label for ="red"><span >University</span></label> <br/>

  <input type="checkbox" id="yellow">
  <label for= "yellow"><span>Friends</span></label> <br/>

  <input type="checkbox" id="blue">
  <label for ="blue"><span>Other</span></label> <br/>

</div>

这是外部.js文件中的角码:

代码语言:javascript
复制
(function() {
    'use strict';

    var exampleApp = angular.module('exampleApp');


    exampleApp.controller('MirrorCheckboxController', ['$scope', function($scope) {
        $scope.settings = [{
            name: 'Weather Forecast',
            value: '',
            id: 'mirror-1'
        }, {
            name: 'Time',
            value: '',
            id: 'mirror-2'
        }, {
            name: 'Traffic Situation',
            value: '',
            id: 'mirror-3'
        }, {
            name: 'Personal Schedule',
            value: '',
            id: 'mirror-4'
        }];

      }]);

})();

我正在寻找一种方法,以隐藏我的“事件”div容器,如果在角循环中的最后一个复选框与id:‘镜-4’被选中。我试图用JQuery在脚本标签中的html文件中解决这个问题。如果代码没有引用由角给出的ID,那么代码是可以工作的。为什么下面的代码不起作用?

代码语言:javascript
复制
<script>
    $('#mirror-4').change(function(){
          if($(this).prop("checked")) {
            $('#events').show();
          } else {
            $('#events').hide();
          }
        });
</script>

我对棱角很陌生,对每一次帮助都心存感激。

你好,洛伦兹

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-01-13 17:48:13

如果你使用角度,你应该这样做,并尽量避免jQuery。正如上面的注释中所提到的,尝试使用角度逻辑来处理您的请求。这是一个非常基本的角度示例

将添加到复选框

代码语言:javascript
复制
<input type="checkbox" ng-model="toggleEvents" ng-attr-id="{{ setting.id }}">
// If you want to display the "events container" 
// per default add a `ng-init` like this:
<input type="checkbox" ng-model="toggleEvents" ng-init="toggleEvents=true" /> 

ng-if 使用来处理隐藏/显示:

代码语言:javascript
复制
<div ng-if="toggleEvents">  
    This is your Eventbox
</div> 

或者,为了更接近给定的代码,您可以这样做:

代码语言:javascript
复制
exampleApp.controller('MirrorCheckboxController', ['$scope', function($scope) {
    // we use this to collect the checked checkboxes
    $scope.checkedElements = {};

    // your settings
    $scope.settings = [{
      name: 'Weather Forecast',
      value: '',
      id: 'mirror-1'
    }, {
      name: 'Time',
      value: '',
      id: 'mirror-2'
    }, {
      name: 'Traffic Situation',
      value: '',
      id: 'mirror-3'
    }, {
      name: 'Personal Schedule',
      value: '',
      id: 'mirror-4'
    }];

    // the method to display or hide the event container
    $scope.showEvents = function(obj) {
      return (!obj['mirror-4']);
    }
});

在你的html中:

代码语言:javascript
复制
<div ng-controller="MirrorCheckboxController">
   <p ng-repeat="setting in settings">
       <label class="checkbox">
           <input type="checkbox" 
             ng-id="{{setting.id}}" 
             ng-model="checkedElements[setting.id]" />
           {{setting.name}}
       </label>
   </p>
   <div ng-show="showEvents(checkedElements)">
    <!-- your event container -->
    This is shown per default, hide if checkox with id "mirror-4" is checked
   </div>
</div>

还有这个就在这里的小提琴。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41639939

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档