如何在 ng-option 上使用 ng-click(或其他赋值方式)

How to use ng-click on ng-option( or other way to assign value)

如何使用ng-options.

$scope.fieldTable = [
    { 
        filed:"text",
        title:"Global"
    },
    {
        field: "relatedentity",
        title: "Entity"
    },
    {
        field:"title",
        title:"Title"
    },
    {
        field: "content",
        title: "Content"
    }
]

我想构建一个使用显示的标题,当 select 某些内容时,弹出一个显示相应字段的警报 window。最初的 selection 是

{ 
    filed:"text",
    title:"Global"
}

有人能帮忙吗?

您可以使用 ng-change 来做到这一点

<select ng-options="item.title as item.title for item in fieldTable track by item.title" ng-model="selected" ng-change="onChanged()">

然后在控制器的 onChange() 方法中,您可以做任何您想做的事 :) 在您的情况下,显示值为

的警报

编辑: https://plnkr.co/edit/4csDtFVH5mKd7Xr39WtG?p=preview

var app = angular.module('stack', []);

app.controller('MainCtrl', function($scope) {
  $scope.fieldTable = [{
    field: "text",
    title: "Global"
  }, {
    field: "relatedentity",
    title: "Entity"
  }, {
    field: "title",
    title: "Title"
  }, {
    field: "content",
    title: "Content"
  }];

   $scope.selected = $scope.fieldTable[0];

  $scope.hasChanged = function() {
alert($scope.selected.field);
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

<body ng-app="stack" ng-controller="MainCtrl">
   <select ng-options="item.title for item in fieldTable" ng-model="selected" ng-change="hasChanged()">
  </select>
</body>