Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/408.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 加载时绑定ng模型中的ng选项(如双向绑定)_Javascript_Angularjs - Fatal编程技术网

Javascript 加载时绑定ng模型中的ng选项(如双向绑定)

Javascript 加载时绑定ng模型中的ng选项(如双向绑定),javascript,angularjs,Javascript,Angularjs,情况: 我有以下选择: <select ng-model="model" ng-options="o as o.name for o in options track by o.code"> <option value="">- Default -</option> </select> 我想做什么: 我想用预先选择的值进行选择。我的限制是我只知道对象的code属性。借助track by符号,我可以这样做: $scope.options =

情况:

我有以下选择:

<select ng-model="model" ng-options="o as o.name for o in options track by o.code">
    <option value="">- Default -</option>
</select>
我想做什么:

我想用预先选择的值进行选择。我的限制是我只知道对象的
code
属性。借助
track by
符号,我可以这样做:

$scope.options = [{id: 1, code: 'foo', name: 'Foo!'}, {id: 2, code: 'bar', name: 'Bar!'}];
$scope.model = {code: 'bar'};
它工作时,选择的选择值是“Bar!”

问题:

当我将此数据发送到后端时,我需要发送对象的
id
属性。发送的数据是
{code:'bar'}
,但不是
{id:2,code:'bar',name:'bar!'}

对我来说这是正常的行为。。。因为我存储在我的模型
{code:'bar'}
中,并且没有更改所选的值

结论:

模型
中存在默认值时(当使用
跟踪方式
表示法存在匹配时),是否有方法告知AngularJS将值从
选项
列表复制到
模型

信息:我知道我可以这样做
$scope.model=$scope.options[2]
(用一些逻辑来确定索引…),但我想要更神奇的东西。。。如果可能的话…:D


您需要在
上添加
ng change
,然后创建一个函数来查找所选选项及其对应的JSON模型。用这个

HTML

无论何时选择
框中的选项,您都会看到控制台中打印的该选项的实际JSON对象

这是工作票

操作信息:我知道我可以做这样的$scope.model=$scope.options[2](用一些逻辑来确定索引…),但我想要更神奇的东西。。。如果可能的话…:D


我为你的3件物品准备了3个魔法

魔术1: 魔术2:
检查:如果可能的话,通过
id
跟踪可以解决您的问题。我不知道
id
,所以我不能这样做:
$scope.model={id:2}如果他没有选择任何选项会发生什么?如果他选择“-Default-”,那么它将为空,因为它没有任何与之关联的JSON结构。实际上,我的问题发生在我没有触摸选择并提交表单时,因此此解决方案无法解决问题。谢谢你在上面花费的时间:)好的,谢谢,希望我能在不久的将来解决你的下一个问题。为什么循环和过滤器这么长。看看我的答案。就这么简单。作为一名开发人员,我们应该能够尽可能避免循环。没有循环的代码总是最优的,并且比有循环的代码更好。想想什么时候下拉列表会有1000个选项。我认为最好的选项是第一个。没有我想象的那么神奇;)但我认为没有其他选择了。。。非常感谢。顺便说一句,在你的
magic3
中,我认为休息是没有用的。您不能使用break-in
angular.forEach()
中断
<div ng-app='app' ng-controller='mainCtrl'>
    <select ng-model="model" ng-options="o as o.name for o in options track by o.code" 
    ng-change='getValue(this)'>
    <option value="">- Default -</option>
</select>
</div>
angular.module('app',['QuickList']).controller('mainCtrl', function($scope){
   $scope.options = [{id: 1, code: 'foo', name: 'Foo!'}, {id: 2, code: 'bar', name: 'Bar!'}];
   $scope.model = {code: 'bar'};
   $scope.getValue = function(item){
      console.log($scope.selectedOption = item.model);
   }
});
$scope.model = $scope.options.filter(function(item) {
  return item.code=== 'bar';
})[0];
app.filter('getById', function() {
  return function(input, id) {
    var i=0, len=input.length;
    for (; i<len; i++) {
      if (+input[i].id == +id) {
        return input[i];
      }
    }
    return null;
  }
});

$scope.model  = $filter('getById')($scope.options, 2);
angular.forEach($scope.options, function(option) {
        $scope.model = option.name == "name";
        if($scope.model !=null){break}
    });