简体   繁体   English

角度ng-hide用于动态创建的元素

[英]angular ng-hide for dynamic created element

I have some HTML table on the UI which is created dynamically. 我在UI上有一些动态创建的HTML表。 There is a condition in which when user will click on a button in a table row then that button will hide itself and some div element will be displayed.I have used ng-show and ng-hide property for that. 有一种情况,当用户点击表格行中的按钮时,该按钮将隐藏自身并显示一些div元素。我使用了ng-show和ng-hide属性。 But it is not working as desired. 但它没有按预期工作。

Table : 表:

Id   Column1     Column2    Action
1    somevalue  somevalue  <Button>
2    somevalue  somevalue  <Button>

Html of Button Element :(where Id is table first column value) 按钮元素的Html :(其中Id是表格第一列值)

<button ng-show="hidepenality' + Id + '" type="button" class="btn btn-primary btn-xs" ng-model="btnChangePenality" ng-click="ChangePenalityButton(' + Id + ')" >Change Penality</button>

Html of radio button div : (where Id is table first column value) 单选按钮div的html :(其中Id是表第一列值)

<div ng-hide="hidepenality'+Id+'"><label class="c-radio" data-ng-repeat="panalityType in penalityTypeList"><input type="radio" name="typepenality" data-ng-model="typepenality.Id" data-ng-value="{{panalityType.Id}}" /><span>{{panalityType.Type}}</span></label> </div>

Now, when I am clicking on first row button then logic should only hide first row button and only display first row radio div (which is by default hide). 现在,当我点击第一行按钮时,逻辑应该只隐藏第一行按钮并且只显示第一行无线电div(默认为隐藏)。 Below is my logic : 以下是我的逻辑:

  $scope.ChangePenalityButton = function (Id) {

                var scope = $parse('hidepenality' + Id)
                scope.assign($scope, false);

        }

But when I am clicking on first row button then button is hiding for both rows, when it should just for first row as per the logic. 但是,当我点击第一行按钮时,按钮会隐藏两个行,当按照逻辑时它应该只是第一行。

Please help!!!! 请帮忙!!!!

It because ng-hide="hidepenality'+Id+'" is not correct angular expression. 因为ng-hide =“hidepenality'+ Id +'”不是正确的角度表达式。 I don't think angular will be able to parse it. 我认为angular不会解析它。 You should do it like in snipper below. 你应该像下面的snipper一样。 Basically setting flag to every row and then just changing this flag value from true to false and so on. 基本上将标志设置为每一行,然后将此标志值从true更改为false,依此类推。

 angular.module('app', []); angular.module('app').controller('Example', function () { this.rows = [ {name: 'blah1', buttonShown: true}, {name: 'blah2', buttonShown: true}, {name: 'blah3', buttonShown: true} ]; }); 
 table { border: 1px solid black; width: 100%; } 
 <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.5/angular.js"></script> <div ng-app="app" ng-controller="Example as Example"> <table> <tr ng-repeat="row in Example.rows"> <td>{{row.name}}</td> <td> <button ng-show="row.buttonShown" ng-click="row.buttonShown = false">hide</button> <div ng-hide="row.buttonShown"> some div <br> <button ng-click="row.buttonShown = true">show</button> </div> </td> </tr> </table> </div> 

Edit: To answer your comment, that is interesting cos it sure fails here: 编辑:要回答你的评论,这很有趣,因为它确实在这里失败:

 <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.5/angular.js"></script> <div ng-app> <button ng-click="hidepenality'+Id+'">error here</button> </div> 

As for dynamic data, it does not matter how array was created, you can add property to each value if you wish so. 对于动态数据,如何创建数组无关紧要,如果您愿意,可以为每个值添加属性。 There is other solution if you don't want to do that, under assumption that each row has unique id, you can create flags object on controller and use it to keep track of show/hide flags. 如果您不想这样做,还有其他解决方案,假设每行都有唯一的ID,您可以在控制器上创建标志对象并使用它来跟踪显示/隐藏标志。

 angular.module('app', []).controller('Example', function () { this.flags = {}; this.rows = [ {name: 'blah1', id: 102}, {name: 'blah2', id: 4}, {name: 'blah3', id: 3} ]; }); 
 table { border: 1px solid black; width: 100%; } 
 <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.5/angular.js"></script> <div ng-app="app" ng-controller="Example as Example"> <table> <tr ng-repeat="row in Example.rows"> <td>{{row.name}}</td> <td> <button ng-hide="Example.flags[row.id]" ng-click="Example.flags[row.id] = true">hide</button> <div ng-show="Example.flags[row.id]"> some div <br> <button ng-click="Example.flags[row.id] = false">show</button> </div> </td> </tr> </table> </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM