I am new to working with Angular and I have a function that I need help with. The goal is to select a random name from an array and display it.
Within my controller, I have defined an array called names
. My intention is to pass this array into the $scope.message
function and then call it in my HTML.
However, I keep coming across an error message:
"Error: [$interpolate:interr] Can't interpolate:
{{message(names)}}
{{}}
This is how my controller looks:
eventsApp.controller("EventController", function EventController($scope)
{
var names = ["David", "Tony", "Tim", "David", "Daniel", "Tom"];
var randomChoose = function(array){
return Math.floor(Math.random() * array.length-1);
};
$scope.message = function(array){
var name = array.indexOf(randomChoose(array));
return "Hello"+name;
};
});
And here is a snippet of my HTML code:
<div class="container">
<div ng-controller="EventController">
{{message(names)}}
{{}}
</div>
</div>
I appreciate any assistance. I have attempted to change names
to $scope.names
but it did not fix the issue.