Locate a specific item in an array using AngularJs based on a key and present its value on the View

Imagine you have an array of objects:

$scope.objArr = [{key:1,value:'value1'},{key:2,value:'value2'},{key:3,value:'value3'}];

And a variable that corresponds to key. For instance:

$scope.a = 3;

In the Controller, you want to display the value in the View. In this case, it should show "value3."

Although using ng-repeat works, it may not feel right. Is there a better way to achieve this without creating another function?

UPDATE:

This is the complete code snippet being used:

<body ng-app="myApp">
    <div ng-controller="MyController">
        <span ng-repeat="obj in objArr | filter:{key:a.test}:true">{{obj.value}}</span>
        <input ng-model="a.test"/>
    </div>
</body>

Controller section:

var myApp = angular.module('myApp',[]);

myApp.controller('MyController',function($scope){
  $scope.objArr = [{key:1,value:'value1'},{key:2,value:'value2'},{key:3,value:'value3'}];
  $scope.a = {test:2}
});

Initially, it displays "value2", but it stops showing any value after changing the input field. What could be the reason for this?

Answer №1

Give this method a shot

You have the option to establish a function within your controller

 var myApp = angular.module('myApp',[]);

 myApp.controller('MyController',function($scope){
    $scope.objectsArray = [{id:1,name:'name1'},{id:2,name:'name2'},{id:3,name:'name3'}];
    $scope.searchFunction = function(id) {
    var result = filterFilter($scope.objectsArray, {id: id});
    $scope.item= result[0].name;
  };
});

When it comes to your View, implement the following code snippet

<div ng-controller="MainController">
  <input ng-model="search"/>
  <button ng-click="searchFunction(search);">Search</button>
 {{item}}  
</div>

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

Switching on the click functionality

I was able to successfully toggle a boolean variable along with some other options. However, I encountered an issue when trying to create another click function for a different button to set the boolean variable to false. Here is my code: let manageme ...

Tips on resolving issues with cellclickable functionality in Angular with gridster2

VERSION: ^9.3.3 HTML <button (click)="toggleEditing()">{ editing ? 'cancel' : 'editing' }</button> <button>ADD</button> <gridster [options]="options"> &l ...

What is the best way to enable the user to scroll smoothly while new data is continually being added to the screen?

I'm attempting to develop a chat feature where the scroll automatically moves down when new messages are received by the user. However, I've come across an issue while trying to allow users to manually scroll up. Every time I scroll up and a new ...

Collect form input data containing a binary image and showcase it in a view using webapi

I am currently working on a webapi project using MVC architecture. The project involves converting a jpg image to binary data, sending it to a SQL server, and then retrieving the form data along with the image back as a jpg for display on the view. Althoug ...

Is there a way to obtain an Instagram login token through AJAX?

I'm encountering issues while trying to receive a token from Instagram. Despite using the following code, I keep running into these errors: SEC7127: CORS request blocked the redirect. SCRIPT7002: XMLHttpRequest: Network Error 0x2ef1, Unable to final ...

Is it possible to access forms and input fields in an AngularJS script without having to pass them from the HTML code?

Seeking a solution for input field validation, I have written code where input field states are passed from HTML to a script through a function. However, my goal is to directly retrieve the values in the script without passing them from the HTML when calli ...

Cannot Display CSS Background Image Locally

Apologies for the simple inquiry, but I am facing an issue with displaying my background image using background-image:url() in the CSS. Strangely, it does not work with this method, but when I use content:url(); it works just fine. Interestingly, backgrou ...

Having trouble uploading a file to Laravel using the jQuery .post method?

Here is a snippet of my HTML code: <input type="file" class="form-control" name="file-name" id="file-id"> This is the JavaScript function that is meant to send the file to an API: var file = $('input#file-id').val(); $.post('my/url/ ...

Wait for the successful $save in AngularJS before redirecting

How can I redirect to my list page after POSTing a form if there are no errors and the data is successfully saved in the database? app.controller('NoteCreateController', ['$scope', 'Note', '$routeParams', '$l ...

Exploring the world of nested routes in Angular and how to efficiently

Hey there! I'm brand new to all of this and still trying to wrap my head around a few things, so any guidance you can offer would be awesome! :) Overview I've got a bunch of projects (/projects) Clicking on a project takes me to a detailed sum ...

Troubleshooting: Why the TableTools basic usage example is not functioning as

After replicating the code from http://datatables.net/release-datatables/extensions/TableTools/examples/simple.html in my Visual Studio project, I organized the files by saving two css files as style1.css and style2.css, along with three js files named sc ...

Transferring information from a service to an AngularJS controller

I have a service that retrieves data from a URL provided as a parameter, and it is functioning correctly. However, when attempting to pass this data to a controller's $scope in AngularJS, I am not receiving any data. var app = angular.module("Recib ...

Eliminating rows from a DataTable through an Ajax request

I have a DataTables table from datatables.net with a custom column containing icons for various actions. One of these actions is deleting a row without reloading the data. I'm looking for a built-in function to remove datatable rows locally after dele ...

Displaying dates on the Amcharts category axis for instances with empty data

I am currently creating a fruit consumption chart for each day, and so far everything is working correctly in the provided example. var chart = AmCharts.makeChart("chartdiv", { "type": "serial", "hideCredits": true, "fixedColumnWidth": '10px& ...

Using jQuery to crop an SVG view box

I'm currently working on a sticker website project for a client who requires the ability to crop an image within a specific SVG shape, resembling a Halloween face (shown below). The uploaded image should be displayed only within this shape while hidi ...

Error encountered when calling the function .jstree() on a tree structure

While working on MVC4, I attempted to implement a jstree but encountered an issue where the tree view was not displayed when running my application. Upon investigating with Firebug, I came across the following error message: "TypeError: $(...).jstree is ...

Need to know how to show a DIV for a specific time frame using Javascript?

One of the features I am looking to implement in my Django application is a display that notifies the user when one of their posts is about to start. The idea is to show a DIV element with the message: <div>“Will start soon”</div>, 15 minut ...

What is the reason that utilizing $("input").first() does not function properly on Google Forms?

While practicing with Selenium, I encountered an issue while using this Google form. Specifically, I found that I couldn't select the first input field by using $("input").first() To confirm this issue, I turned to the web browser (Firefox) console t ...

Arranging currency values in Angular's UI-Grid widget

Need help sorting a grid column with dollar values that include $ and , as the sorting is not functioning correctly. Is there a solution to make the sorting work, or should I convert the values to numbers and then display them as money? Here is what I cur ...

Animation of scrolling in a horizontal layout

Currently, I am utilizing ng-repeat 1.4 to dynamically generate elements within a div that has polymer shadow classes and uses the 'layout horizontal' style, resulting in code similar to: <div class='layout horizontal'> < ...