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

最后都变了- 提交于 2019-12-03 23:44:36

问题


How to use ng-options.

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

I want to build a which use the title as displayed and when select something, popout a alert window which display the according field. The initial selection is

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

Can anyone help?


回答1:


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>



回答2:


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



来源:https://stackoverflow.com/questions/29519805/how-to-use-ng-click-on-ng-option-or-other-way-to-assign-value

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