1

Why function getPrice('Table') doesn't work in controller? It return undefined instead 400. That quite works in plain JS (without $scope).

function MyCtrl($scope) {   

$scope.prices = [{
    name: 'Bed',
    price: 900
}, {
    name: 'Table',
    price: 400
}];


$scope.getPrice = function (name) {
    $scope.prices.forEach(
    function (el) {
        if (el.name == name) {
            return el.price;
        } else {
            return null;
        }
    }
    );
}
};
1
  • 1
    $scope.getPrice has no return statement, so it returns nothing. Commented Feb 20, 2014 at 11:34

1 Answer 1

1

Your getPrice function does not return anything. Try this one :

 $scope.getPrice = function (name) {
             $price = null;
             $scope.prices.forEach(
                     function (el) {
                         if (el.name == name) {
                             $price =  el.price;
                             return;
                         } else {
                             return;
                         }
                     }
             );
             return $price;
         }
Sign up to request clarification or add additional context in comments.

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.