我正在使用ng-repeat来迭代列表对象。当选择一个选项时,我希望该对象不是单个变量值而且也不使用ng-model。因为ng-model必须只有id.I想要传递subapp whenClick(subapp)
中的对象http://jsfiddle.net/KN9xx/1416/
HTML:
<div ng-app="myapp" ng-controller="FirstCtrl">
<select class="form-control selectpicker" data-live-search="true" ng-model="subappData" id="subapplicationid" style="height: 21px;width: 300px;-moz-margin-start: 132px;margin-left: 138px;margin-top: 2px;-moz-margun-start: 132px;">
<option value="">Select</option>
<option ng-repeat='subapp in subapplicationList | filter:query' data-select-watcher data-last="{{$last}}" value = "{{subapp.ID}}"> <a href="#"> {{subapp.ID}} - {{subapp.DESCRIPCION}} </a> </option>
</select>
<div>
<div>
<button ng-click="whenClick()">Click</button>
</div>
</div>
</div>
控制器:
var myapp = angular.module('myapp', []);
myapp.controller('FirstCtrl', function ($scope) {
$scope.subapplicationList = [
{"ID" : 9 ,"DESCRIPCION" : "a","value" : 24 },
{"ID" : 1 ,"DESCRIPCION" : "b","value" : 7 },
{"ID" : 2 ,"DESCRIPCION" : "c","value" : 12 },
{"ID" : 3 ,"DESCRIPCION" : "d","value" : 2 },
];
$scope.whenClick = function() {
alert(JSON.stringify($scope.subappData));
};
});
myapp.directive('selectWatcher', function ($timeout) {
return {
link: function (scope, element, attr) {
var last = attr.last;
if (last === "true") {
$timeout(function () {
$(element).parent().selectpicker('val', 1);
$(element).parent().selectpicker('refresh');
});
}
}
};
});