0

Hello I am trying to use Angularjs and I'm not very good at it. I'm trying to find something from the madeUpCards[] array. using the find() function of javascript. I am not entirely sure, I think it's not working when I use it with Angularjs. here is the my Code:

<body ng-app="myApp" ng-controller="myCtrl">

<h3>{{getCardById('14')}}</h3>
</body>

array here:

    $scope.madeUpCards = [
     {
        "id": "23",
        "name": "The brain",
        "closed": true,
    },
     {
        "id": "2",
        "name": "Portal dead",
        "closed": true,
    },
     {
        "id": "14",
        "name": "Holiday",
        "closed": true,
    },
     {
        "id": "13",
        "name": "warded",
        "closed": true,
    },
];

javascript :

const app = /**
* myApp Module
*
* Description
*/
angular.module('myApp', []).controller('myCtrl', ['$scope', function($scope){

    $scope.getCardById = function(id) {
        this.id = id
        let foundCard = $scope.madeUpCards.find(function(card, index){
            return card.id == this.id
        });
    return foundCard.name;
    }

}]);

in the console this appears:

 angular.js:15536 TypeError: Cannot read property 'find' of undefined
  at ChildScope.$scope.getCardById ((index):49)
  at fn (eval at compile (angular.js:16387), <anonymous>:4:234)
  at expressionInputWatch (angular.js:17398)
  at Scope.$digest (angular.js:19095)
  at Scope.$apply (angular.js:19463)
  at bootstrapApply (angular.js:1945)
  at Object.invoke (angular.js:5122)
  at doBootstrap (angular.js:1943)
  at bootstrap (angular.js:1963)
  at angularInit (angular.js:1848)

pleas help me how to fix this, or atleast tell me what's wrong.

3
  • what is Cards here passed through HTML getCardById(Cards, '14')? Commented Jan 22, 2019 at 13:41
  • what is your expected output for your foundCard? Commented Jan 22, 2019 at 13:45
  • the (Cards, '14') is the $scope.madeUpCard array, I would like to get the card as object with id of 14. Commented Jan 22, 2019 at 14:09

3 Answers 3

1

Change const madeUpCards to $scope.Cards in your controller, and instead of passing in Cards, just use <h3>{{ getCardById('14') }}</h3>

Then in your controller, use $scope.Cards. i.e. In the controller:

$scope.Cards = [
     {
        "id": "23",
        "name": "The brain",
        "closed": true,
    },
     {
        "id": "2",
        "name": "Portal dead",
        "closed": true,
    },
     {
        "id": "14",
        "name": "Holiday",
        "closed": true,
    },
     {
        "id": "13",
        "name": "warded",
        "closed": true,
    },
];

...

$scope.getCardById = function(id) {
    let foundCard = $scope.Cards.find(function(card, index){
        return card.id == this.id
    });
    return foundCard.name;
}

In the HTML:

<body ng-app="myApp" ng-controller="myCtrl">

<h3>{{ getCardById('14') }}</h3>
</body>

Now you could still pass in Cards to getCardById, but it's already accessible in your controller so it would be pointless.


AngularJS will only bind elements to the DOM that are defined on the scope.

You created Cards as a local variable in the controller, but not part of the scope. So in the HTML when you pass Cards into a function, its undefined (not part of the scope).

This passes undefined into your controller, and then you attempt to call find on undefined, hence your error.

Sign up to request clarification or add additional context in comments.

9 Comments

should I include $scope.Cards inside the controller?
@Vanz yes, I would, especially if it's a constant or returned from the server.
Sure thing - I'm on my way into work, so it'll be 20min before I can look for sure, but try just using id instead of this.id. That seems strange to me.
I remove the this.id = id, and then "card.id == id" it works, Big thanks!!!. but what's the difference of using "this.id" and directly "id"? it works and I dont know how. thanks again
@Venz this keyword is used for when you want properties on an object (has nothing to do with angular really). Because you're simply in a function, this really has no meaning. It's possible that this.id is not a property so even though you write this.id = id it leaves this.id as undefined. In your case there's no real reason to use this.id because you just want the value from the view. Great I'm glad its working!
|
0

Cards is undefined, try that

<h3>{{ getCardById([
     {
        "id": "23",
        "name": "The brain",
        "closed": true,
    },
     {
        "id": "2",
        "name": "Portal dead",
        "closed": true,
    },
     {
        "id": "14",
        "name": "Holiday",
        "closed": true,
    },
     {
        "id": "13",
        "name": "warded",
        "closed": true,
    },
], '14') }}</h3>

Comments

0
<h3>{{ getCardById(Cards, '14') }}</h3>

According to this snippet, the variable Cards should be binded to $scope.

So, define a $scope.Cards at the initial part of the controller as below:

const app = /**
* myApp Module
*
* Description
*/
angular.module('myApp', []).controller('myCtrl', ['$scope', function($scope){

    $scope.Cards = [
        {
            "id": "23",
            "name": "The brain",
            "closed": true,
        },
        {
            "id": "2",
            "name": "Portal dead",
            "closed": true,
        },
        {
            "id": "14",
            "name": "Holiday",
            "closed": true,
        },
        {
            "id": "13",
            "name": "warded",
            "closed": true,
        },
    ];

    $scope.getCardById = function(cards, id) {
        this.cards = cards
        this.id = id
        let foundCard = this.cards.find(function(card, index){
            return card.id == this.id
        });
        return foundCard;
    };

}]);

Happy coding.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.