javascript - 根据 Angularjs 中选定的值显示值

标签 javascript angularjs

您好,我有以下数据

var myApp = angular.module('myApp', [])
    .controller('TestController', ['$scope', function ($scope) {
        $scope.data = [{"city":"New York","location":"123", "sales" :"233.00"},
                {"city":"Chicago","location":"953", "sales":"455"}'
                {"city":"New York","location":"788",  "sales" :"23432.23"},
                {"city":"Chicago","location":"853"}];

  $scope.update=function(location){

  };
}]);   

在我的 html 中我有

  <td>
    <select ng-model="item.location" ng-options="c.location as c.city for c in data" 
     ng-change="update(c.location)"></select>
  </td>
  <td>
    <div>
      <span ng-model="item.sales">{{sales }} </span>
    </div>
  </td>

我正在尝试从下拉列表的选定值中调用更新函数,并根据所选的项目,我想使用跨度更新销售值。我不知道如何完成那部分。请让我知道该怎么做。谢谢

最佳答案

您甚至不需要更新功能,只需使用数据绑定(bind)即可。像这样更改您的 ng-options 以绑定(bind)整个对象,而不仅仅是一个字段 location :

ng-options="c as c.city for c in data"

要显示销售额,只需执行以下操作:

<span>{{ item.location.sales }} </span>

DEMO

关于javascript - 根据 Angularjs 中选定的值显示值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22727655/

相关文章:

java - Android 4.0 -> 4.3(包括在内)- WebView 页面之间的网络存储丢失

javascript - 将子路由设为默认

javascript - Kendo UI Grid 重新加载数据错误

javascript - 为什么复制的 HTML 看起来不像原始 HTML?

javascript - 递归 promise 链

javascript - 在 $resource 中使用更新方法 'PUT'

angularjs - 具有多个标记的 ng-map - 仅显示一个带缩放的标记

AngularJS ng-options 创建范围

javascript - 如何在焦点事件下处理点击事件

javascript - 为什么在我的 Jasmine 测试中我的变量未定义 - Jasmine.js