I'm completely new to AngularJs and still trying to grasp the concepts. I have a few questions that I hope someone can help me with:
- Is it possible to trigger code in the controller only when a button is clicked, rather than having it load with the view?
- Or does the controller always load with the view, making it impossible to isolate certain functions to onclick events?
- What is the best way for me to create a function that will only execute when a button is clicked, and not every time the page loads? (In an AngularJs manner)
This is the HTML section:
<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>
And 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 this to be triggered only on onclick
$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);
})
}
})