0

I'm just new to AngularJs and haven't understand the whole thing yet, I have a lot of questions and these are the following:

  1. I want to hit the code on the controller during onclick only, Is it possible?
  2. Or it's impossible since the controller is always loaded when the view loads?
  3. What's the best way for me to create a function that will only hit during onclick of the button? And not everytime the page loads. (AngularJs way)

This is my html:

<ion-view view-title="Account Fixer"> <ion-content class="padding"> <div class="list"> <label class="item item-input item-stacked-label"> <span class="input-label">IrId</span> <input type="text" placeholder="irid" value="{{irid}}" disabled> </label> <label class="item item-input item-stacked-label"> <span class="input-label">Email</span> <input type="text" placeholder="Enter New Email" value="{{email}}"> </label> <button class="item button button-block button-positive" ng-click="postForm()">Fix Issue</button> </div> </ion-content> </ion-view> 

This is my controller:

.controller('TestCtrl',function(Test,$scope, $http, $stateParams){ $scope.email = Test.get('AB12345').email; $scope.irid = Test.get('AB12345').irid; //I want to hit this during onclick ONLY $scope.postForm = function(dataForm){ var encodedString = 'action=' + encodeURIComponent("QA_fixEmail") + '&irid=' + encodeURIComponent(Acct.acctData().irid) + '&email=' + encodeURIComponent(dataForm.email) + '&password=' + encodeURIComponent("abcd1234"); $http({ method: 'POST', url: 'someservice', data: encodedString, headers: {'Content-Type': 'application/x-www-form-urlencoded'} }) .success(function(data,status){ console.log(status); }) .error(function(data, status){ console.log(status); }) } }) 
2
  • What you've shown so far should work just fine. In what way is it not working? Commented May 19, 2016 at 2:31
  • It's working, what I want to achieve is, to hit the $scope.postForm ONLY during button click from my html Commented May 19, 2016 at 2:33

1 Answer 1

1

Q: Is it possible ... to [hit a function] during onclick only?

A: Yes, of course. Just use ngClick

Here are some examples:

AngularJS Events

Note that one link uses ng-click, and the other link uses ngClick. These are equivalent:

AngularJS, ng-click vs ng:click vs ngClick

Sign up to request clarification or add additional context in comments.

5 Comments

sir paul, but why is it when I place a breakpoint on the code it hits the $scope.postForm = function() even if I didn't click the button yet
Because DEFINIING a JS function (done when the code is first parsed) is different from EXECUTING a function (done only when it's called - e.g. by the click event). Put your breakpoint INSIDE postForm and you should see what I mean. 'Hope that helps :)
so this means, it only initialized the function? but not necessarily execute did i get your explanation right?
Yes, exactly. BTW: your code looks fine, and it looks like you're already familiar with ngClick ;)
yes i've used ngClick on my other pages. thanks btw, i thought during the hit, it was executing... Thanks!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.