如何使用ng-click ng-option(或其他方式分配值)

时间:2015-04-08 16:06:51

标签: angularjs ng-options

如何使用ng-options。

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

我想构建一个使用显示的标题,当选择某个内容时,弹出一个显示相应字段的警告窗口。最初的选择是

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

有人可以帮忙吗?

2 个答案:

答案 0 :(得分:21)



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>
&#13;
&#13;
&#13;

答案 1 :(得分:3)

您可以使用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