Reputation: 1221
I want to fire some jQuery code when you click on a checkbox. The problem is that when I click on a selectbox first time when the page is loaded, nothing happens. But when I click again, the jQuery-code is executed. I've tried to set angular.element(ready) as below, but it dont work:
angular.element(document).ready(function() {
$http.get($rootScope.appUrl + '/nao/test/test')
.success(function(data, status, headers, config) {
$scope.test = data.data;
});
$scope.testa = function() {
$('.checkboxfisk').click(function() {
var fish = $(this).attr('id');
alert(fish);
});
};
});
<tr ng-repeat="info in test"><td>{{info.stracka}}</td><td>{{info.tid}}</td><td><input type="checkbox" id="{{info.id}}" class="checkboxfisk" ng-click="testa()"></tr>
Upvotes: 4
Views: 12503
Reputation: 1
I am not sure if this is the right way, but the below code worked for me:-
$timeout(function(){
if(condition){
/*
code you want to execute */
}
});
Upvotes: 0
Reputation: 20014
The Angular way to render items is different from "On DOM Ready" that is why we need to treat these as 2 separate things.
Angular could render items after DOM is ready, this could happen for example if there is an AJAX call($http.get
) and that is why a directive may be the recommended approach.
Try something like this:
<body ng-controller="MainCtrl">
<input type="checkbox" chk-Sample="" >
<script>
var myApp = angular.module('myApp', []);
myApp.controller('MainCtrl', ['$scope', function ($scope) {}]);
myApp.directive("chkSample", function() {
return {
restrict: "A", //A - means attribute
link: function(scope, element, attrs, ngModelCtrl) {
$(element).click(function() {
var fish = $(this).attr('id');
alert(fish);
});
}
};
});
...
By declaring the directive myApp.directive("chkSample",...
as an attribute chk-Sample=""
every time angulare generates the input element it will execute the link function in the directive.
Upvotes: 0
Reputation: 1
The direct reason why it runs after second click is that in your ng-click handler you use jQuery click() function, which binds the click handler, within you execute alert. So after first click, you only bind the function, but not execute it. After it is bound, the next click executes it. Anyway, like it was said before, it should be done using directives.
Upvotes: 0
Reputation: 245429
The proper way to interact with the DOM in Angular is to create your own custom directive. You can then wire up the jQuery handler in the postLink function of you directive (which Angular runs when it builds the page on load). Your handler will be there and waiting when the page is ready.
Take a look at Angular's documentation for directives and give it a shot:
AnuglarJS: Developer Guide: Directives
Looking at your example, though, that may be too much. There's no need to have jQuery listen for the click event at all. You just need to remove the jQuery wrapper around your function definition and let Angular handle the click event itself:
$scope.testa = function(id) {
alert(id);
};
And then modify your ng-click
attribute to pass the id in the expression:
<input type="checkbox"
id="{{info.id}}"
class="checkboxfisk"
ng-click="testa(info.id)">
Upvotes: 2