2016-11-04 28 views
0

我有一个包含两个属性的对象列表:label和description。在这个名单上的HTML我循环:AngularJS-ng-repeat对象

<select class="form-control" 
    ng-options="fid.label for fid in myFidList" 
    ng-model="fidSelected" 
    ng-change="setFidDescription()"/> 

我试图做的是调用一个函数(setFidDescription),以获得我已经在选项标签中选择的对象。所以我可以在另一个div中更改相应的解释。这似乎不工作:ng模型似乎不更新。功能:

$scope.setFidDescription = function() { 
    console.log($scope.fidSelected); 
} 

此代码打印一个空的对象。 我错过了什么?另一种方式可以得到我选择的美元指数。

更多信息: JSON + console

更新: 我发现我的问题。看到这里的解释:

AngularJS ngModel doesn't work inside a ui-bootstrap tabset

这是我的问题。

+0

发布您的json数据 – Sajeetharan

+0

您是否正在获取控制台权限数据:console.log($ scope.fidSelected); ? – Jigar7521

+0

不,我打印一个没有属性的对象 – Massimo

回答

1

// Code goes here 
 

 
angular 
 
    .module('myApp', []) 
 

 
.controller('testController', ['$scope', 
 
    function($scope) { 
 

 
    $scope.myFidList = [{ 
 
     label: 'label1', 
 
    }, { 
 
     label: 'label2', 
 
    }] 
 

 
    $scope.setFidDescription = function() { 
 
     alert($scope.fidSelected.label); 
 
    } 
 

 
    } 
 
])
<!DOCTYPE html> 
 
<html data-ng-app="myApp"> 
 

 
<head> 
 
    <link rel="stylesheet" href="style.css"> 
 
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.8/angular.min.js"></script> 
 
    <script src="script.js"></script> 
 
</head> 
 

 
<body data-ng-controller="testController"> 
 

 

 

 
    <select class="form-control" ng-options="fid.label for fid in myFidList" ng-model="fidSelected" ng-change="setFidDescription()"></select> 
 

 
</body> 
 

 
</html>

希望这会帮助你。

+0

thansk作为回复。但我一直在“未定义”。 fidSelected是空的。 – Massimo

+0

@Massimo,在我的例子中呢? –

+0

是的,用你的代码:[示例](https://i.imgsafe.org/c3964603f4.png)。注意:ng-repeat工作正常 – Massimo