[英]Angular $scope has push() as undefined
Here is my HTML: 这是我的HTML:
<input ng-blur="CheckUser()" name="username" ng-model="RegisterFormData.username"
class="form-control"/>{{ a.check_username_result }}
The controller and ng-app are assigned, since every part of the application is working and the only missing part is that I cannot send back data after $http
request to the page, it says property of push()
is undefined 控制器和ng-app已分配,因为应用程序的每个部分都在工作,而唯一缺少的部分是我无法在
$http
请求发送到页面后发送回数据,它说push()
属性未定义
Now, here is my controller and CheckUser()
is the event which sends a certain data back to the page after some operation, though all operations are done properly, except the push() which errors as undefined: 现在,这是我的控制器,
CheckUser()
是在执行某些操作后将某些数据发送回页面的事件,尽管所有操作均已正确完成,但push()除外,其错误为未定义:
LoginApp.controller("RegisterController", function($scope, $http, registerService){
var username = null;
var password = null;
RegisterData = $scope.RegisterFormData;
$scope.CheckUser = function(RegisterData){
username = $scope.RegisterFormData.username;
console.log(username);
usernameResult = registerService.CheckUser(username);
if( usernameResult == "exists")
$scope.a.push({check_username_result : "Username already exists, Choose another."});
else if( usernameResult == 'fresh')
$scope.a.push({check_username_result : "Username available!"});
else
$scope.a.push({check_username_result : "Error. try again."});
}; // end of user_check()
$scope.registerSubmit = function(){
username = $scope.RegisterFormData.username;
password = $scope.RegisterFormData.password;
};
});
Just declare $scope.a
as an array, then you can push to it. 只需将
$scope.a
声明为一个数组,然后就可以推送到它。
$scope.a = [];
$scope.CheckUser = function(RegisterData){
// ...
You cannot call functions on an object before it it defined. 您不能在对象定义之前调用函数。 You can do
你可以做
$scope.a=[];
And then 接着
$scope.a.push({check_username_result : "Username already exists, Choose another."});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.