上HTML發送錯誤味精這是我service.js有沒有辦法通過服務
我加入childname只aaray如果他們是獨一無二的,如果沒有的話我想味精顯示錯誤我的html頁面
app.factory('DomainNameService',['$q', function($q) {
setChildBD: function(childBD){
var deferred = $q.defer();
//I am using this to check unique name gets added to childDomainName array
if(childDomainName.indexOf(childBD)==-1)
childDomainName.push(childBD);
else {
//I want to display this message as the error message on html page
deferred.reject('Name already in use');
}
},
//From controller when get method is called it returns the last value stored, hence not displaying the error msg
getChildBD: function(){
return childDomainName;
},
}])
這是我需要顯示錯誤味精我的html頁面:
<form name="businessdomain" role="form" ng-controller="domainController">
<label for="name" >
Name</label>
<! I want to show the error msg beside the input box -->
<input focus="" class="form-control ng-pristine ng-invalid ng-invalid-required" id="name" name="name" placeholder="Name" ng-model="busdomain.name" ng-change="domainNameChanged()" required="true" >
<!--On Ok click calling my controller method -->
<button type="submit" ng-click="addSubTree(createdomain.$valid)" id="btnData">OK</button>
這是我的控制器:
.controller('domainController', ['$scope', '$state', 'DomainNameService', function($scope, $state, DomainNameService) {
$scope.addSubTree = function(val){
var busDomain=$scope.busdomain.name;
DomainNameService.setChildBD(busDomain);
$scope.childBD=DomainNameService.getChildBD();
$scope.currentBDStatement.push($scope.busdomain.name); $scope.currentDomainName=$scope.getBusDomain($scope.childBD,parent,varType);
$scope.statements.push($scope.currentDomainName);
//Some code here
$state.go('BusDomainTree', null, { reload: true });
}
有沒有辦法使用服務發送錯誤消息。提前致謝。!
你包括DomainNameService在你的網域控制器在控制器中注入這家工廠,然後?如果是這樣,我們需要看到這個控制器代碼 –
爲什麼在世界上你使用承諾? – sirrocco