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

ぃ、小莉子 提交于 2019-12-01 02:34:40

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>

You can use ng-change to do that

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

then in your onChange() method in the controller you can do whatever you want it to do :) In your case , show an alert with the value

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!