Ng-options Doesn't Show The Ng-model Object In The Select Box On Load (angularjs) November 20, 2023 Post a Comment I have a html select input. Here I use AngularJS: https://jsfiddle.net/Lt0cuwLd/6/Below is the only code I have added. It just go through each item in the list and compare its 'Tekst' value with t.selected's 'Tekst' value. If they both equal then, it will return that object and we assign this to t.selected. Now t.selected's '$hash' value also matches and not just 'Tekst' After all computer is not clever as humans :)$scope.t.selected = $scope.t.Valgalternativer.reduce(function(curr, next){ if(next.Tekst === $scope.t.selected.Tekst) returnnext; elsereturn curr; }, {}) CopySolution 2: You need init your variable t.selected like this $scope.t.selected = $scope.t.Valgalternativer[0];.Live example on jsfiddle.angular.module('app', ['ngAnimate']) .controller('controller', function($scope) { $scope.t = { "Tabellnavn": "BE3.2:1", "Siffer": "x", "Stikkord": "Utvendig kledning:", "Tekst": { "Format": 0, "Content": "{Matrise BE3.2:1}" }, "Merknader": null, "Delprodukter": null, "Valgalternativer": [{ "Siffer": "-", "Tekst": "{Matrise BE3.2:1}", "Tekst2": null, "Kode": null, "Delalternativer": null }, { "Siffer": "00", "Tekst": "Valgfri", "Tekst2": null, "Kode": null, "Delalternativer": null }, { "Siffer": "11", "Tekst": "Murt forblending", "Tekst2": null, "Kode": "NB2.7---x-", "Delalternativer": null }, { "Siffer": "21", "Tekst": "Bordkledning på vegg utvendig – stående bord", "Tekst2": null, "Kode": "QK2.11xx---", "Delalternativer": null }, { "Siffer": "22", "Tekst": "Bordkledning på vegg utvendig – stående ukantede bord", "Tekst2": null, "Kode": "QK2.12x---", "Delalternativer": null }, { "Siffer": "23", "Tekst": "Bordkledning på vegg utvendig – stående spaltekledning", "Tekst2": null, "Kode": "QK2.15xx---", "Delalternativer": null }, { "Siffer": "24", "Tekst": "Bordkledning på vegg utvendig - liggende bord", "Tekst2": null, "Kode": "QK2.21xx---", "Delalternativer": null }, { "Siffer": "26", "Tekst": "Platekledning på vertikal flate utvendig utvendig", "Tekst2": null, "Kode": "QK5.226--", "Delalternativer": null }, { "Siffer": "31", "Tekst": "Kledning med tynnplatekassetter av kopper", "Tekst2": null, "Kode": "SM4.2--", "Delalternativer": null }, { "Siffer": "32", "Tekst": "Kledning med tynnplatekassetter av titansink", "Tekst2": null, "Kode": "SM4.3--", "Delalternativer": null }, { "Siffer": "33", "Tekst": "Kledning med tynnplatekassetter av aluminium", "Tekst2": null, "Kode": "SM4.4--", "Delalternativer": null }, { "Siffer": "34", "Tekst": "Kledning med tynnplatekassetter av varmforsinket stål", "Tekst2": null, "Kode": "SM4.5--", "Delalternativer": null }, { "Siffer": "35", "Tekst": "Kledning med plane metallplater", "Tekst2": null, "Kode": "SM5.1", "Delalternativer": null }, { "Siffer": "88", "Tekst": "Uten utvendig kledning", "Tekst2": null, "Kode": null, "Delalternativer": null }, { "Siffer": "99", "Tekst": "Annen utvendig kledning – må spesifiseres", "Tekst2": null, "Kode": null, "Delalternativer": null }], "selected": { "Siffer": "99", "Tekst": "Annen utvendig kledning – må spesifiseres", "Tekst2": null, "Kode": null, "Delalternativer": null } }; $scope.t.selected = $scope.t.Valgalternativer[0]; }) .filter('matriseValgFilter', function() { returnfunction (option) { if (!isNaN(option.Siffer) && option.Kode != null){ return option.Siffer + ' - ' + option.Tekst + ' (' + option.Kode + ')'; } if (!isNaN(option.Siffer)){ return option.Siffer + ' - ' + option.Tekst; } return option.Tekst; } });Copy<scriptsrc="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script><linkhref="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.6/css/bootstrap.min.css" /><scriptsrc="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script><scriptsrc="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.6/js/bootstrap.min.js"></script><linkhref="https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.4.0/animate.min.css" /><scriptsrc="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.8/angular-animate.min.js"></script><bodyng-app="app"><divng-controller="controller as c"style="padding:10px;"><selectclass="form-control"ng-model="t.selected"ng-options="option | matriseValgFilter for option in t.Valgalternativer" ></select><prestyle="margin-top: 10px;"> {{t.selected | json}} </pre></div></body>CopyUpdatedOh... I understand. You define t.selected in your variable t. But it doesn't work, because { "Siffer": "99", "Tekst": "Annen utvendig kledning – må spesifiseres", "Tekst2": null, "Kode": null, "Delalternativer": null }!= { "Siffer": "99", "Tekst": "Annen utvendig kledning – må spesifiseres", "Tekst2": null, "Kode": null, "Delalternativer": null } CopyAnother word are not equal.$scope.t.Valgalternativer[$scope.t.Valgalternativer.length-1]!=$scope.t.selected CopyTry this$scope.t.selected = .Valgalternativer[$scope.t.Valgalternativer.length-1]; CopySolution 3: editAs per your comment, for this to work, one way is to set the selected property like this.$scope.t.selected =$scope.t.Valgalternativer[2]; CopyIf the object comes from another service or json, you may have to find/filter from t.Valgalternativer and then assign appropriate index.This is so because, as pointed out in other answer, if you initialize selected the way you are doing, it will be a new object whose hash value is different. If you like this approach, please remove the "selected" property from t and add it separately as above. There is no need to change any markup.initialTry this<selectclass="form-control" ng-model="t.selected" ng-options="option | matriseValgFilter as option | matriseValgFilter for option in t.Valgalternativer" ng-change="change(t.selected)" ng-init='t.selected = "99 - Annen utvendig kledning – må spesifiseres"'> </select> CopyThe viewValue of the option you choose should match with value of one of the options of select.as syntax is used to set value for options in select. Documentation here.Solution 4: None of the answers worked properly in my actual application, but solved it using a method in the controller that I call with ng-init. Ended up with this: <selectclass="form-control" ng-init="loadMatriseValg(node, t)" ng-model="t.selected" ng-options="option | matriseValgFilter for option in t.Valgalternativer" ng-change="changeMatrise(node, t.selected, '{{t.selected.Kode}}')"></select> CopyThen in the controller I have this: $scope.loadMatriseValg = function (node, t) { // user has not selected anything, set default valueif (!t.selected) { t.selected = t.Valgalternativer[0]; } // loop through all valg alternativerfor (var v = 0; v < t.Valgalternativer.length; v++) { if (t.Valgalternativer[v].Siffer == t.selected.Siffer) { t.selected = t.Valgalternativer[v]; return; } } } CopyNew fiddle here: https://jsfiddle.net/fiddlejan/wng2genw/ Share Post a Comment for "Ng-options Doesn't Show The Ng-model Object In The Select Box On Load (angularjs)" Top Question Creating Popup Window With Form Content And Then Show Output In Parent Page And Send To Database I have a table and in of it's column I want user when c… Get The Value Of Checked Radio Button Without Re-selecting The Buttons So I am getting some buttons as follows: var reportFilterBu… JQuery Val Is Undefined? I have this code: But when i write $('#editorT Sol… Onsubmit Method Doesn't Stop Submit My onsubmit is not working. My idea was to put some mandato… "too Much Recursion" Error When Calling JSON.stringify On A Large Object With Circular Dependencies I have an object that contains circular references, and I w… Configuring Any Cdn To Deliver Only One File No Matter What Url Has Been Requested I am currently working on a new project where the entire pa… How To To Insert TradingView Widget Into React Js Which Is In Script Tag Link: Https://www.tradingview.com/widget/market-overview/ export default class extends Component { render() { … Display Modal Form Before User Leaves Page I've used window.onbeforeunload to display a custom mes… React Mobx - Store Return Proxy Object I have the following state class: import { observable, acti… How To Change Color Of Specific Elements In Array I am trying to build a sorting Visualizer using React. Righ… December 2024 (1) November 2024 (39) October 2024 (73) September 2024 (25) August 2024 (378) July 2024 (341) June 2024 (721) May 2024 (1296) April 2024 (820) March 2024 (1581) February 2024 (1732) January 2024 (1395) December 2023 (1450) November 2023 (435) October 2023 (620) September 2023 (278) August 2023 (336) July 2023 (264) June 2023 (355) May 2023 (201) April 2023 (121) March 2023 (153) February 2023 (182) January 2023 (245) December 2022 (126) November 2022 (241) October 2022 (177) September 2022 (165) August 2022 (482) July 2022 (287) June 2022 (249) Menu Halaman Statis Beranda © 2022 - JavaScript Download