sending an array from Angular controller

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.

Answer №1

It appears that your approach to randomness is not quite correct. Here's a functional 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;
    };

}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Creating an interactive dropdown feature using AngularJS or Ionic framework

$scope.AllCities = window.localStorage.getItem['all_cities']; <div class="row"> <div class="col"> <div class="select-child" ng-options="citie.name for citie in AllCities" ng-model="data.city"> <label&g ...

How to efficiently remove duplicate items from multiple select dropdowns in Angular using ng-repeat?

Need help with dynamically assigning objects to select boxes in AngularJS. I have a scenario where I add multiple select boxes to a form, but each box should only display items that haven't been selected in other boxes. How can I achieve this functio ...

What is the best approach for utilizing the map function to render JSON data within a JavaScript function in ReactJS?

Currently, I am utilizing the material-ui library to generate card-like items. The list of these items is stored in a JavaScript file as shown below: var Items = [ { name: "Tandoori Pizza", image: "Images/pizza.png", price ...

Struggling to locate items within the table using protractor

Having trouble finding the element in the table using protractor and bluebird promise concepts Utilizing angular JS on an HTML page: <tr data-ng-repeat="tableRow in $ctrl.tableResults track by $index" id="case-list-table-row" class="ng-scope" style="" ...

Is there a reason why this MD Bootstrap Snippet isn't functioning properly?

When zooming out to 25% or beyond, I noticed that the toolbar unexpectedly pops open and refuses to close. How can I prevent this from happening? I asked this question yesterday but unfortunately received only downvotes :( Appreciate any help provided ...

Dealing with the hAxis number/string dilemma in Google Charts (Working with Jquery ajax JSON data)

My Objective I am attempting to display data from a MySQL database in a "ComboChart" using Google Charts. To achieve this, I followed a tutorial, made some modifications, and ended up with the code provided at the bottom of this post. Current Behavior T ...

Looking to populate my HTML fields with values

When attempting to edit, the id is being passed but the data fails to populate in the HTML fields. Please refer to this image. Index @{ ViewBag.Title = "AngularJs Tutorial"; } <style> </style> <h2>AngularJs Tutorial : CRUD operatio ...

Most efficient method for creating matrices using C++

I have a query that I couldn't find a direct answer to anywhere else. In C language, I create 2D arrays using pointers of pointers and dynamic memory allocation. Now, as I transition to C++, I am contemplating whether it's better to utilize stand ...

Creating a dynamic parameter name within the state: A step-by-step guide

My goal is to dynamically write errors to the state without hardcoding parameter names. I have an object in state called errors: {} where I aim to record errors as they appear while filling out the form. For example, if there is an error in the email fiel ...

AngularJS display element within viewport

My div is extremely wide and contains many elements. I need a way to bring specific elements into view when a button is clicked. Is there an alternative to $anchorscroll that can achieve this? Looking for a solution for horizontally scrolling to a particu ...

Transform JSON data into a formatted HTML table

I've been attempting to convert my JSON data from a URL into an HTML Table, but the table ends up empty. The JSON file is stored at . Below is the code that I used: <html> <head> <script type="text/javascript" src="https://ajax.go ...

Is there a way to update a JSON key using the "onchange" function in React?

I'm facing an issue. I have a form with two inputs. The first input is for the key and the second input is for the value. I need to update the values in the states whenever there is a change in the input fields, but I'm unsure of how to accomplis ...

Tips for obtaining results from a File Reader

I am currently facing an issue with an onchange event in my HTML. The event is intended to retrieve an image from the user, convert it to a data URL, and then send it over socket.io to store in the database. However, I am struggling to figure out how to ac ...

Transferring information between a pair of input fields using ngModel

There are two input fields named input1 and input2. An event has been created where anything typed in input1 is displayed in input2. However, if something is manually changed or typed into input2, the event should not trigger. I think I may need to use a ...

Create a MySQL query for every element present in a given array

Within my system, there is an array that holds three values. These values are directly linked to a user's account, used for referencing recovery questions based on their unique ID. After a user submits their email address through a form, the system v ...

My HTML elements are not displaying in the correct order. Any suggestions on how to resolve

Hi, I am currently working on a website using HTML, CSS, and JS. I have encountered a small issue in the HTML section. Here is the code snippet: window.onscroll = function() { changeOnScroll() }; function changeOnScroll() { var winScroll = docum ...

Discover the power of Material UI combined with React Router to create dynamic BreadCrumbs with unique

I am facing a challenge while trying to incorporate material-ui breadcrumbs with reactRouter. The issue arises because the script is unable to find the correct string for ':id' in the breadcrumbs when it is not specified. (It works as intended wh ...

The Angular JS controller does not function correctly when it is wrapped within $(function () {});

After enclosing the controller code within the $(function () {}); method, it ceased to function properly. Below is an example of the code: Contacts.cshtml @{ Layout = null; } <!DOCTYPE html> <html> <head> <meta name="viewpor ...

Creating an event listener with a dynamic name

I'm in the process of developing a customizable button bar where I need to attach onclick event listeners to each button dynamically. The functions to be assigned should have names provided by the button's data-function attribute. … <div cl ...

Tips for using jasmine-ajax with grunt-contrib-jasmine in your workflow

I've been experimenting with utilizing the jasmine-ajax library for simulating ajax requests within grunt-contrib-jasmine, but it appears that jasmine is having trouble locating the library (indicating jasmine.Ajax is not defined). This is an excerpt ...