Table of Contents
回复内容:
Home Backend Development PHP Tutorial javascript - AngularJS操作ng-show指令

javascript - AngularJS操作ng-show指令

Jun 06, 2016 pm 08:14 PM
css javascript node.js php

需求:页面有一个使用ng-repeat指令生成的列表,当点击一个item时,就隐藏之。

问题:请问如何在ng-click的回调中操作当前item的ng-show的值,而不影响其他item?

代码:

<ul ng-controller="listCtrl">
    <li ng-repeat="item in items" ng-click="delete()" ng-show="show">
        <span ng-bind="item.title"></span>
        <span ng-bind="item.price"></span>
    </li>
</ul>
<script>
    var myApp = angular.module('myApp',[]);
    myApp.controller('listCtrl',function($scope){
        var items = [{title:"title1",price:120},{title:"title2",price:23},{title:"title3",price:32234}]
        $scope.items = items;
        // $scope.show = "true";
        $scope.delete = function(item){
            // $scope.show = "false"
        }
    })
</script> 
Copy after login
Copy after login

回复内容:

需求:页面有一个使用ng-repeat指令生成的列表,当点击一个item时,就隐藏之。

问题:请问如何在ng-click的回调中操作当前item的ng-show的值,而不影响其他item?

代码:

<ul ng-controller="listCtrl">
    <li ng-repeat="item in items" ng-click="delete()" ng-show="show">
        <span ng-bind="item.title"></span>
        <span ng-bind="item.price"></span>
    </li>
</ul>
<script>
    var myApp = angular.module('myApp',[]);
    myApp.controller('listCtrl',function($scope){
        var items = [{title:"title1",price:120},{title:"title2",price:23},{title:"title3",price:32234}]
        $scope.items = items;
        // $scope.show = "true";
        $scope.delete = function(item){
            // $scope.show = "false"
        }
    })
</script> 
Copy after login
Copy after login

<code><ul ng-controller="listCtrl">
    <li ng-repeat="item in items" ng-click="delete()" ng-show="item.show">
        <span ng-bind="item.title"></span>
        <span ng-bind="item.price"></span>
    </li>
</ul>
<script>
    var myApp = angular.module('myApp',[]);
    myApp.controller('listCtrl',function($scope){
        var items = [{title:"title1",price:120},{title:"title2",price:23},{title:"title3",price:32234}]
        $scope.items = items;
        // $scope.show = "true";
        $scope.delete = function(item){
            $scope.item.show = "false"
        }
    })
</script> </code>
Copy after login

设置一个hidden属性,结合列表的$index即可。

<code>  <ul ng-controller="listCtrl">
    <li ng-repeat="item in items" ng-click="change($index)" ng-hide="hidden.value == $index">
        <span>{{item.title}}</span>
        <span ng-bind="item.price"></span>
    </li>
</ul></code>
Copy after login
<code>angular.module('myApp',[])
  .controller('listCtrl',function($scope){
        var items = [{title:"title1",price:120},{title:"title2",price:23},{title:"title3",price:32234}]
        $scope.items = items;
        $scope.hidden = {value: null}
        // $scope.show = "true";
        $scope.change = function(i) {
          $scope.hidden.value = i;
        }
    })</code>
Copy after login

之所以要把hidden设置为对象而非原始值,是因为ng-repeat有独立的作用域,直接在ng-repeat的作用域上修改原始值父控制器上的值是不会改变的;需要借助引用类型,如对象或数组,才能保证修改保存到父级控制器。

http://jsbin.com/modute/edit?html,js,output

@onelove 这哥们儿的回答应该是这个意思:

<ul ng-controller="listCtrl">
    <li ng-repeat="item in items" ng-click="delete(item)" ng-show="item.show">
        <span ng-bind="item.title"></span>
        <span ng-bind="item.price"></span>
    </li>
</ul>
<script type="text/javascript">
    var myApp = angular.module('myApp',[]);
    myApp.controller('listCtrl',function($scope){
        var items = [{title:"title1",price:120,show:true},{title:"title2",price:23,show:true},{title:"title3",price:32234,show:true}]
        $scope.items = items;
        $scope.delete = function(item){
            item.show = false;
        }
    })
</script> 
Copy after login

这样虽然能实现需求,但是问题很大,就是把控制视图的show和数据耦合在一起了,这样不好吧?还有什么办法么?

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Two Point Museum: All Exhibits And Where To Find Them
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

Dynamic web page elements XPath and Class names change frequently. How to stably crawl the target a tag? Dynamic web page elements XPath and Class names change frequently. How to stably crawl the target a tag? Apr 01, 2025 pm 04:12 PM

Dynamic web element crawling problem: dealing with XPath and Class name changes, many crawler developers will encounter a difficult problem when crawling dynamic web pages: the goal...

From PHP to Go or Front-end? The suggestions and confusions of reality from experienced people From PHP to Go or Front-end? The suggestions and confusions of reality from experienced people Apr 01, 2025 pm 02:12 PM

Confusion and the cause of choosing from PHP to Go Recently, I accidentally learned about the salary of colleagues in other positions such as Android and Embedded C in the company, and found that they are more...

What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? Apr 03, 2025 am 12:03 AM

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

Is Debian Strings compatible with multiple browsers Is Debian Strings compatible with multiple browsers Apr 02, 2025 am 08:30 AM

"DebianStrings" is not a standard term, and its specific meaning is still unclear. This article cannot directly comment on its browser compatibility. However, if "DebianStrings" refers to a web application running on a Debian system, its browser compatibility depends on the technical architecture of the application itself. Most modern web applications are committed to cross-browser compatibility. This relies on following web standards and using well-compatible front-end technologies (such as HTML, CSS, JavaScript) and back-end technologies (such as PHP, Python, Node.js, etc.). To ensure that the application is compatible with multiple browsers, developers often need to conduct cross-browser testing and use responsiveness

How to convert XML to PDF on Android phone? How to convert XML to PDF on Android phone? Apr 02, 2025 pm 09:51 PM

Converting XML to PDF directly on Android phones cannot be achieved through the built-in features. You need to save the country through the following steps: convert XML data to formats recognized by the PDF generator (such as text or HTML); convert HTML to PDF using HTML generation libraries such as Flying Saucer.

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

How to solve the problem of style loss after Django project is deployed to Pagoda panel? How to solve the problem of style loss after Django project is deployed to Pagoda panel? Apr 01, 2025 pm 09:09 PM

Detailed explanation of style loss after Django project is deployed to pagoda panel. After deploying Django project to pagoda panel, you may encounter style loss problem. This...

See all articles