简体   繁体   English

角度添加到收藏夹

[英]Angular add to favourites

I have a list of items, and on each item I have a button where I can click "Add to favourites" and then a variable should be set to true, and a class is enabled.. 我有一个项目列表,每个项目上都有一个按钮,可以单击“添加到收藏夹”,然后将一个变量设置为true,并启用一个类。

in my controller, I've set $scope.isFavourite = false - When clicking a button I set the $scope.isFavourite = true 在我的控制器中,我设置了$ scope.isFavourite = false-单击按钮时,我设置了$ scope.isFavourite = true

The problem with this is that all items on the page gets the class 问题是页面上的所有项目都获得该类

The code here: HTML: 此处的代码:HTML:

<button ng-click="addToFav()" ng-class="{'active' : isFavourite}"><i class="ion-heart"></i> Netto</button>

And in my controller: 在我的控制器中:

$scope.addToFav = function () {
$ionicPopup.show({
  title: 'Tilføj til favoritter',
  subTitle: 'Ønsker du at tilføje denne butik til dine favoritter?',
  scope: $scope,
  buttons: [
    { text: 'Nej' },
    {
      text: '<b>Ja</b>',
      type: 'button-positive',
      onTap: function(e) {
        $scope.isFavourite = true;
      }
    }
  ]
});

}; };

The problem is that when clicking, it doesn't isolate it to the button you've clicked, but at a global level instead which means that every button in my list gets the class="active", where it only should be the button i've clicked 问题是单击时不会将其隔离到您单击的按钮上,而是在全局级别上显示,这意味着列表中的每个按钮都具有class =“ active”类别,而该类别仅应为该按钮我点击了

您将要研究Angular组件(可以在此处找到一些文档。基本上,一个组件是具有自己作用域的可重用代码段,因此对一个组件所做的更改不会影响其他组件。我会去更详细,但我不像Angular 2一样了解Angular。

By using scope: $scope you are setting the scope to your surrounding controller scope, so all of your popups sharing the same isFavourite variable. 通过使用scope: $scope您可以将范围设置为周围的控制器范围,因此所有共享相同isFavourite变量的弹出窗口。

You could do something like this: 您可以执行以下操作:

<div ng-repeat="item in items">
    <button ng-click="addToFav(item)" ng-class="{'active' : item.isFavourite}"><i class="ion-heart"></i> Netto</button>
</div>

controller: 控制器:

$scope.addToFav = function (item) {
    $ionicPopup.show({
        title: 'Tilføj til favoritter',
        subTitle: 'Ønsker du at tilføje denne butik til dine favoritter?',
        scope: $scope,
        buttons: [
            { text: 'Nej' },
            { text: '<b>Ja</b>',
                type: 'button-positive',
                onTap: function(e) {
                    item.isFavourite = true;
                }
            }
        ]
    });

Working plunker: 工作钻头:

http://codepen.io/kbkb/pen/ONzgBQ?editors=1111 http://codepen.io/kbkb/pen/ONzgBQ?editors=1111

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

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