使用angular.js使用工厂方法调用一个函数时出现以下错误。
TypeError: focusField.clearBorderColor is not a function
at n.$scope.clearField (http://oditek.in/Gofasto/controller/profileController.js:142:14)
at fn (eval at <anonymous> (http://oditek.in/Gofasto/js/angularjs.js:212:283), <anonymous>:4:242)
at f (http://oditek.in/Gofasto/js/angularjs.js:252:485)
at n.$eval (http://oditek.in/Gofasto/js/angularjs.js:132:452)
at n.$apply (http://oditek.in/Gofasto/js/angularjs.js:133:175)
at HTMLInputElement.<anonymous> (http://oditek.in/Gofasto/js/angularjs.js:253:36)
at HTMLInputElement.m.event.dispatch (https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js:4:8549)
at HTMLInputElement.r.handle (https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js:4:5252)
我正在解释下面的代码。
var dashboard = angular.module('Channabasavashwara');
dashboard.controller('profileController', function ($scope, $http, $location, $window, $state, focusField) {
$scope.buttonName = "Add";
$scope.addProfileData = function (billdata) {
//console.log('button name',$scope.buttonName);
if ($scope.buttonName == "Add") {
if (billdata.$valid) {
if ($scope.colgname == null || $scope.colgname == '') {
alert('College Title field could not be blank...');
focusField.borderColor('procolgtitle');
} else if ($scope.shortname == null) {
alert('subtitle field could not be blank...');
} else if ($scope.address == null) {
alert('Address field could not be blank...');
} else if ($scope.contno == null) {
alert('Contact no field could not be blank...');
} else if ($scope.colgemail == null) {
alert('Email address field could not be blank...');
} else if ($scope.colgcode == null) {
alert('college code field could not be blank...');
} else {
var userdata = {
'colg_name': $scope.colgname,
'colg_shname': $scope.shortname,
'address': $scope.address,
'cont_no': $scope.contno,
'email': $scope.colgemail,
'code': $scope.colgcode
};
//console.log('userdata',userdata);
$http({
method: 'POST',
url: "php/profile/addProfileData.php",
data: userdata,
headers: {'Content-Type': 'application/x-www-form-urlencoded'}
}).then(function successCallback(response) {
console.log('profile', response);
alert(response.data['msg']);
$scope.colgname = null;
$scope.address = null;
$scope.contno = null;
$scope.shortname = null;
$scope.colgemail = null;
$scope.colgcode = null;
$scope.profileData.unshift(response.data);
//$state.go('dashboard.profile',{}, { reload: true });
}, function errorCallback(response) {
alert(response.data['msg']);
$state.go('dashboard.profile', {}, {reload: true})
})
}
} else {
alert('This form is not valid');
}
}
}
$scope.clearField = function (id) {
focusField.clearBorderColor(id);
}
});
我在$scope.clearField
函数中遇到错误。我可以正确执行focusField.borderColor('procolgtitle');
。在这个函数中我没有收到任何错误。但是当调用clearField
函数时它会抛出以上错误。
dashboard.factory('focusField', function ($timeout, $window) {
return {
borderColor: function (id) {
$timeout(function () {
var element = $window.document.getElementById(id);
if (element) {
element.focus();
element.style.borderColor = "red";
}
});
},
clearBorderColor: function (id) {
$timeout(function () {
var element = $window.document.getElementById(id);
console.log('ele', element);
if (element) {
element.style.borderColor = "#555555";
}
});
}
}
});
请帮我解决此错误。