I'm new to Angular. I've got a function here where I want to choose a random name from inside the array to display.
In my controller, I have set up an array called names
. I then want to pass it into my $scope.message
function and have it called from the my html.
I keep getting an error:
"Error: [$interpolate:interr] Can't interpolate:
{{message(names)}}
{{}}
my controller:
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;
};
});
My html:
<div class="container">
<div ng-controller="EventController">
{{message(names)}}
{{}}
</div>
</div>
Thank you. I tried to changing names
to $scope.names
but that didn't seem to work.
Your randomness logic seems wrong, a working example :
function EventController($scope) {
$scope.names = ["David", "Tony", "Tim", "David", "Daniel", "Tom"];
var randomChoose = function(array){
return array[Math.floor(Math.random() * array.length)];
};
$scope.message = function(array){
var name = randomChoose(array);
return "Hello " + name;
};
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.