W
W
westdp2016-03-24 12:53:04
JavaScript
westdp, 2016-03-24 12:53:04

How to copy an element on click in Angular?

Hello guys, tell me, I started learning angular and decided to immediately practice it, I chose to do it like a calorie calculator
Tell me how can I make it copy to the right column when clicking on an element with a filter (left column)
Working example: angular.andriyboyko.com

function SearchCtrl($scope, $filter)
{
    $scope.items = [
        {kkal:405, name:'Apple'},
        {kkal:195, name:'Orange'},
        {kkal:105, name:'Banana'},
        {kkal:155, name:'Cherry'},
        {kkal:65, name:'Melon'},
        {kkal:125, name:'Kiwi'},
        {kkal:80, name:'Lime'},
        {kkal:140, name:'Lemon'},
        {kkal:120, name:'Mango'},
        {kkal:40, name:'Olive'},
        {kkal:210, name:'Plum'},
        {kkal:310, name:'Ebony'},
        {kkal:50, name:'Pineapple'}];
    
    $scope.items2 = $scope.items;
    
    $scope.$watch('search', function(val)
    { 
        $scope.items = $filter('filter')($scope.items2, val);
    });
};

Thanks for any help.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey, 2016-03-24
@westdp

1) by ngClick we call some controller method
2) in the controller method we change the state (we transfer the item from one array to another or copy it or whatever you need to do there).
3) the view magically rebuilds itself by changing the state of the data, glory to declarativeness!
In short, the bottom line is that in Angular you have to think about data and managing the state of your application. And "elements" - you just declaratively describe in templates how the UI should react to changes. For example ngRepeat describes how to output a collection item, it's not the same as a loop.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question