javascript - AngularJS操作ng-show指令
需求:页面有一个使用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>
回复内容:
需求:页面有一个使用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>
<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>
设置一个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>
<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>
之所以要把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>
这样虽然能实现需求,但是问题很大,就是把控制视图的show
和数据耦合在一起了,这样不好吧?还有什么办法么?

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

PHP主要是過程式編程,但也支持面向對象編程(OOP);Python支持多種範式,包括OOP、函數式和過程式編程。 PHP適合web開發,Python適用於多種應用,如數據分析和機器學習。

HTML、CSS和JavaScript在Web開發中的作用分別是:1.HTML定義網頁結構,2.CSS控製網頁樣式,3.JavaScript添加動態行為。它們共同構建了現代網站的框架、美觀和交互性。

PHP適合網頁開發和快速原型開發,Python適用於數據科學和機器學習。 1.PHP用於動態網頁開發,語法簡單,適合快速開發。 2.Python語法簡潔,適用於多領域,庫生態系統強大。

PHP起源於1994年,由RasmusLerdorf開發,最初用於跟踪網站訪問者,逐漸演變為服務器端腳本語言,廣泛應用於網頁開發。 Python由GuidovanRossum於1980年代末開發,1991年首次發布,強調代碼可讀性和簡潔性,適用於科學計算、數據分析等領域。

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP適用於Web開發和內容管理系統,Python適合數據科學、機器學習和自動化腳本。 1.PHP在構建快速、可擴展的網站和應用程序方面表現出色,常用於WordPress等CMS。 2.Python在數據科學和機器學習領域表現卓越,擁有豐富的庫如NumPy和TensorFlow。

在使用CraftCMS開發網站時,常常會遇到資源文件緩存的問題,特別是當你頻繁更新CSS和JavaScript文件時,舊版本的文件可能仍然被瀏覽器緩存,導致用戶無法及時看到最新的更改。這個問題不僅影響用戶體驗,還會增加開發和調試的難度。最近,我在項目中遇到了類似的困擾,經過一番探索,我找到了wiejeben/craft-laravel-mix這個插件,它完美地解決了我的緩存問題。

PHP仍然流行的原因是其易用性、靈活性和強大的生態系統。 1)易用性和簡單語法使其成為初學者的首選。 2)與web開發緊密結合,處理HTTP請求和數據庫交互出色。 3)龐大的生態系統提供了豐富的工具和庫。 4)活躍的社區和開源性質使其適應新需求和技術趨勢。
