Angular Js选择列表完成时的火灾事件

时间:2014-05-14 20:18:49

标签: angularjs controller directive selectlist

我有一个选择列表,我试图在完成渲染时触发事件。我有一个链接到我的JsFiddle代码审查。提前致谢... JsFiddle Example

<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 个答案:

答案 0 :(得分:1)

您可以使用<option>生成ng-repeat个元素,并在使用指令呈现的最后一个项目上触发回调:

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/