Home  >  Q&A  >  body text

angular.js - angularjs 如何根据动态id显示div

我在html里面动态设置了p的id代码如下:


<p ng-repeat="item in items">
    <p id="{{item.name}}" class="ng-hide"> {{item.name}} </p>
<p>

html解析之后能出来动态id的效果,类似于这样:

<p ng-repeat="item in items">
    <p id="name1" class="ng-hide"> name1 </p>
    <p id="name2" class="ng-hide"> name2 </p>
    <p id="name3" class="ng-hide"> name3 </p>
<p>

我想要动态的控制这些p显示还是不显示。我在js里面写了这样的代码:

html:
<input type="button" ng-click="show(item.name)">
js:
$scope.show=function(name){
    document.getElementById(name).style.display = "block";
}

结果并没有实现我想要的效果,还是每次点击都会显示三个p。有什么办法能实现我想要的效果么?

给我你的怀抱给我你的怀抱2713 days ago680

reply all(7)I'll reply

  • 过去多啦不再A梦

    过去多啦不再A梦2017-05-15 17:10:52

    Although I don’t understand why, I changed class="ng-hide" to style="desplay:none" and it worked. . .

    reply
    0
  • 怪我咯

    怪我咯2017-05-15 17:10:52

    Is it possible to use ng-show and ng-hide

    reply
    0
  • 伊谢尔伦

    伊谢尔伦2017-05-15 17:10:52

    Add a field "isShow" to item, the default value is false,

    <p ng-repeat="item in items">
        <p id="{{item.name}}" class="ng-hide" ng-show="item.isShow"> {{item.name}} </p>
    <p>

    When the button is clicked, the value of isShow is reversed.

    html:
    <input type="button" ng-click="show(index)">
    js:
    $scope.show=function(index){
        $scope.items[index].isShow = !$scope.items[index].isShow;
    }

    reply
    0
  • PHP中文网

    PHP中文网2017-05-15 17:10:52

    There are two ways to control the display of elements in angular.js. The first is: ng-show ng-hide and the other is ng-if

    reply
    0
  • 大家讲道理

    大家讲道理2017-05-15 17:10:52

    It is recommended to reorganize your data and use ng-hide to control whether elements are hidden or not:

    `[
        {
            id:'id1',
            name:'name1',
            hide:false //配合nd-hide实现元素隐藏和显示
        },
        {
            id:'id2'
            name:'name2',
            hide:true
        }
        //...
    ]
    
    ----------
    
    
    <p ng-repeat="item in items">
        <p id="item.id" ng-hide="item.hide" ng-bind="item.name"></p>
    <p>`
    写的比较简单,有问题再问

    reply
    0
  • 过去多啦不再A梦

    过去多啦不再A梦2017-05-15 17:10:52

    Add another attribute, write it to the custom attribute of p, and then ng-if makes the judgment?

    reply
    0
  • 阿神

    阿神2017-05-15 17:10:52

    https://github.com/xufei/ng-c...

    reply
    0
  • Cancelreply