Im new to angular and I'm trying to create a simple Signup Form which posts to a database (in json).
If the database write is successful, "this.msg" is set to "Post Data Submitted Successfully!"
If the database write is unsuccessful, "this.msg" is set to "Service does not Exists"
<row ng-controller="appController as app">
<div>
Username : <input ng-model="app.regUsername" />
Password : <input ng-model="app.regPassword" />
<input type="button" value="Register" ng-click="app.postdata(app.regUsername, app.regPassword)" />
</div>
<p>Output Message :{{app.msg}}</p>
</row>
However I cant get this.msg to print out in my HTML. Everything seems to be working fine, no console errors, this.msg exists, the database write is ok.
app.controller('appController', ['$http', function ($http) {
this.name = null;
this.password = null;
this.postdata = function (name, password) {
var data = {
name: name,
password: password
};
$http.post('https://my-project.com/.json', JSON.stringify(data)).then(function (response) {
if (response.data){
this.msg = "Post Data Submitted Successfully!";
console.log(this.msg) //works fine;
}
}, function (response) {
this.msg = "Service does not Exists";
console.log(this.msg) //works fine
});
};
All I can presume is that there is a scope issue and {{app.msg}} is somehow outside of this scope
thischanges the scope everytime