![](/img/trans.png)
[英]Why won't my view template bind to a scope variable with AngularJS?
[英]Angularjs won't bind scope variable with function?
当加载和更改select选项时,Angular似乎没有绑定我的作用域变量。 我有当前从选择元素中选择的ID,我想在其旁边显示名称:
<div ng-app>
<h2>Todo</h2>
<div ng-controller="TodoCtrl">
<select ng-model="myid" ng-options="c.id as c.name for c in cs"></select>
<span ng-bind="getName(myid)"></span>
</div>
</div>
function TodoCtrl($scope) {
$scope.myid = 1;
$scope.cs = [ {id:1, name:'one'},
{id: 2, name:'two'},
{id: 3, name: 'three'}];
$scope.getName = function(id) {
angular.forEach($scope.cs, function(c, index) {
if(c.id == id)
return c.name
});
return "NULL";
}
}
为什么跨度不使用我的范围变量更新? 首次加载时以及任何选择更改时,它始终为null。
你的问题在于你的getName函数看看这个小提琴
基本上我只是用
$scope.getName = function(id) {
return ($scope.cs.filter(function(item){
return item.id==id})[0]||{}).name
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.