0
我是新的離子框架。目前我正在研究離子iOS應用程序。我需要做一些不喜歡的功能。由於我有多個記錄來自網絡服務,對於每條記錄,我必須將類似,不喜歡和評論總數的功能並將這些信息保存到database
。IONIC:喜歡不喜歡`ng-repeat`內的功能
還改變CSS
的喜歡和不喜歡的按鈕。
我不明白如何建立這個功能到我的app.Please幫我做到這一點。
我是新的離子框架。目前我正在研究離子iOS應用程序。我需要做一些不喜歡的功能。由於我有多個記錄來自網絡服務,對於每條記錄,我必須將類似,不喜歡和評論總數的功能並將這些信息保存到database
。IONIC:喜歡不喜歡`ng-repeat`內的功能
還改變CSS
的喜歡和不喜歡的按鈕。
我不明白如何建立這個功能到我的app.Please幫我做到這一點。
試試這個方法:
angular.module('main',[]).controller('mainController', function($scope, $http){
$scope.items = [
{"id":1,"title": "item1", "like": false},
{"id":2,"title": "item2", "like": true},
{"id":3,"title": "item3", "like": false},
{"id":4,"title": "item4", "like": true},
{"id":5,"title": "item5", "like": false},
{"id":6,"title": "item6", "like": true},
{"id":7,"title": "item7", "like": false},
{"id":8,"title": "item8", "like": false},
{"id":9,"title": "item9", "like": true},
{"id":10,"title": "item10", "like": false},
];
$scope.toggle_like = function(item){
//send item.id to HTTP Call to change id in DB and execute following statement in success callback
item.like = !item.like;
}
})
.fake_link:hover{
cursor:pointer;
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.0/angular.min.js"></script>
<div ng-app="main" ng-controller="mainController">
<table>
<thead>
<tr>
<th> Title </th>
<th> Like </th>
</tr>
</thead>
<tbody>
<tr ng-repeat="item in items">
<td> {{item.title}} </td>
<td ng-click="toggle_like(item)" class="fake_link"> <span ng-if="item.like"> Unlike </span> <span ng-if="!item.like"> Like </span> </td>
</tr>
</tbody>
</table>
</div>
這裏是工作提琴,以及: https://jsfiddle.net/Lxwbeeat/
太謝謝你了@ user3597009我會努力讓你知道:) – Nupur
我已經嘗試過它的工作正常,但我也想改變計數增加或減少喜歡和不喜歡。 – Nupur
你可以使用underscore.js,或者你可以用下面的代碼來計算喜歡的東西: (編輯toggle_like函數) function toggle_like(item){ item.like =!item.like; $ scope.liked = $ scope.items.filter(function(item){return item.like;})。length; //和不喜歡的總是$ scope.items.length - $ scope.liked; } – user3597009