'How can I select a selected item from one array from another? AngularJS
Please help, I don't understand how I can select a selected item if it comes from somewhere else and is in a different array. If this is not possible, is it possible to search for an item in the current array and select it as a select item if yes, can you give me an example please. PS: I was able to set an item if I selected it as the original from an existing item like this, but would like to select it from a different array
$scope.initialMention = [
$scope.mentions[0][0],
$scope.mentions[1][0]
];
var app = angular.module('plunker', []);
app.controller('MainCtrl', function($scope) {
$scope.mentions = [
[
{
"name": "[email protected]",
"uuid": "31a52cab-429d-4efa-91dc-d79b154dd4f9",
"type": "Users",
"selected": false,
},
{
"name": "[email protected]",
"uuid": "020e5b7c-98b6-457a-a90e-d78253eef11d",
"type": "Users",
"selected": false,
},
{
"name": "testTeam 😀",
"uuid": "fe5b143c-c6c2-4d34-a152-2a679899541d",
"type": "Teams",
"selected": false,
},
{
"name": "41335412351235 😀",
"uuid": "d751eb81-7363-4f32-a91c-dc6c69d1c113",
"type": "Teams",
"selected": false,
}
],
[
{
"name": "[email protected]",
"uuid": "31a52cab-429d-4efa-91dc-d79b154dd4f9",
"type": "Users",
"selected": false,
},
{
"name": "[email protected]",
"uuid": "020e5b7c-98b6-457a-a90e-d78253eef11d",
"type": "Users",
"selected": false,
},
{
"name": "testTeam 😀",
"uuid": "fe5b143c-c6c2-4d34-a152-2a679899541d",
"type": "Teams",
"selected": false,
},
{
"name": "41335412351235 😀",
"uuid": "d751eb81-7363-4f32-a91c-dc6c69d1c113",
"type": "Teams",
"selected": false,
}
]
]
$scope.initialMention = [
{
"name": "testTeam 😀",
"uuid": "fe5b143c-c6c2-4d34-a152-2a679899541d",
"type": "Teams",
"selected": false,
},
{
"name": "41335412351235 😀",
"uuid": "d751eb81-7363-4f32-a91c-dc6c69d1c113",
"type": "Teams",
"selected": false,
}
];
});
<!DOCTYPE html>
<html ng-app="plunker">
<head>
<meta charset="utf-8" />
<title>AngularJS Plunker</title>
<script>document.write('<base href="' + document.location + '" />');</script>
<link rel="stylesheet" href="style.css" />
<script data-require="[email protected]" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.js" data-semver="1.0.7"></script>
<script src="app.js"></script>
</head>
<body ng-controller="MainCtrl">
<br />
<br />
<br />
<div ng-repeat="mention in mentions">
<select
ng-model="initialMention[$index]"
ng-options="item as item.name for item in mention">
</select>
<br />
{{ initialMention[$index]}}
<br />
{{mention[$index]}}
</div>
</body>
</html>
Solution 1:[1]
$scope.initialMention = $scope.mentions.flat().slice(0,2)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Henry Ecker |