angular.module("MainLogin.controller", [])
// ========= Controllers
.controller("LogInController", function($scope) {
})
.controller("CreateAccountController", function ($scope, $rootScope, createAccountService) {
$rootScope.statusMessage = null;
$scope.ActionCreateAccount = function () {
$scope.statusMessage = createAccountService.createAccount($scope.createAccount);
console.log($scope.statusMessage);
}
})
// <======== Controllers
// ========= Factories
.factory("createAccountService",["$http", function($http, $scope) {
var fac = {};
fac.createAccount = function (accountDetail) {
return $http({
url: '/Account/CreateAccount',
data: { account: accountDetail },
method: 'post'
})
.then(function (response) {
debugger;
if (response.data.status == "1") {
$scope.statusMessage = "Successfully created the account"
alert($scope.statusMessage);
} else {
$scope.statusMessage = response.data.status
alert($scope.statusMessage);
}
})
}
return fac;
}])
Cannot read property 'statusMessage' of undefined"
即使我将$rootScope
更改为$scope
,也会出现错误。我只想将$ scope.statusMessage设置为工厂的结果,这样我就可以将它绑定到我的html。你能指点我,我弄错了。谢谢。
答案 0 :(得分:1)
将您的服务更改为:
.factory("createAccountService",["$http", function($http, $scope) {
var fac = {};
fac.createAccount = function (accountDetail) {
return $http({
url: '/Account/CreateAccount',
data: { account: accountDetail },
method: 'post'
});
}
return fac;
}])
你的控制器:
.controller("CreateAccountController", function ($scope, $rootScope, createAccountService) {
$scope.statusMessage = null;
$scope.ActionCreateAccount = function () {
createAccountService.createAccount($scope.createAccount)
.then(function(response){
if (response.data.status == "1") {
$scope.statusMessage = "Successfully created the account"
alert($scope.statusMessage);
} else {
$scope.statusMessage = response.data.status
alert($scope.statusMessage);
}
console.log($scope.statusMessage);
});
}})