0

In AngularJS, I have a ng-repeat for . Now what I want to do -

On ng-repeat, I want to do ng-init like

<tr ng-repeat="blah in blah" ng-init($event) />

The $event works fine with ng-click($event). How to do the same for ng-init?

4
  • Why would you like to do it ? why $event ? Commented Aug 11, 2016 at 9:13
  • If you need the element in ng-init defined inside the controller then get it via dependency injection $element Commented Aug 11, 2016 at 9:14
  • for each ng-repeat, i want to check if the tr DOM has something in each record and update a button in that record. so to amke that check, i want to make a call. Commented Aug 11, 2016 at 9:15
  • check this if this could help u stackoverflow.com/a/24173892/4817575 Commented Aug 11, 2016 at 9:15

2 Answers 2

1

In HTML

<tr ng-repeat="blah in blahs"  ng-init="$last && done()" />

Inside your controller

function myControllerFunc($scope, $element){
    $scope.done= function(){
       //do something with $element
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

you mean you can handle the elemetn suing $element?
yes $element will be the element to which the controller is bound.
But ng-init does not work because ng-init is called before the ng-repeat finishes. Now how?
Yes your correct ng-init will be called initially, check my edit above does this fix your problem? Now done will be called after the last is encountered
0

In ng-init cannot exists $event. Use a directive.

<div ng-app="myApp" ng-controller="myCtrl">
   <input type="text" my-ng-init="myFunc"><br>
</div>

<script>
var app = angular.module('myApp', []);
app.directive('myNgInit', function() {
  return {
    scope: {
     myNgInit: '&'
    },
    link: function(scope, element, attributes) {
    scope.myNgInit()(element);
    }
  };
});

app.controller('myCtrl', function($scope) {
   $scope.myFunc= function($element) {
    console.log($element);
   }
});
</script>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.