2014-05-14 93 views
0

我有一个选择列表,我尝试在完成呈现时触发事件。我有一个链接到我的JsFiddle代码审查。在此先感谢... JsFiddle ExampleAngular Js选择列表火灾事件完成时

<div ng-controller="MyCtrl"> 
    <select ng-options="size as size.name for size in sizes " ng-model="item" ng-change="update()"></select> 
    {{item.code}} {{item.name}} 
</div> 

JS:

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


function MyCtrl($scope) { 
    $scope.sizes = [ {code: 1, name: 'n1'}, {code: 2, name: 'n2'}]; 
    $scope.update = function() { 
     console.log('77: ' + $scope.item.code, $scope.item.name) 
    } 
} 

回答

1

您可以通过使用ng-repeat产生<option>元素和射击通过使用指令渲染最后一项回调:

myApp.directive('onLastRepeat', function($timeout) { 
    return function (scope, elm, attrs) { 
     if (scope.$last) { 
      $timeout(function() { 
       scope.$eval(attrs.onLastRepeat); 
      }); 
     } 
    }; 
}); 

HTML:

<select> 
    <option ng-repeat="size in sizes" value="{{size.code}}" on-last-repeat="yourCallback()">{{size.name}}</option> 
</select> 

有一个小提琴:http://jsfiddle.net/aartek/G8S32/406/

编辑:

解决方案与所谓的onChange()事件时选择的选项改变:http://jsfiddle.net/G8S32/408/

+0

好了,现在添加,要调用一个事件的能力onChange的选择选项,那么我们有一个赢家!有兴趣看到你的结果。到目前为止,谢谢。 –

+0

@RobertGreen新增。它和你的代码一样。 – akn