这是我的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 });
}
有没有办法使用服务发送错误消息。在此先感谢。!
答案 0 :(得分:0)
在你的工厂里。
app.factory('DomainNameService',['$q', function($q) {
this.error = '';
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
this.error = 'Name already in use';
}
},
}])
在您的HTML中。
<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" >
<span class="error" ng-show="DomainNameService.error">{{DomainNameService.error}}</span>
请确保您已将此工厂注入控制器,然后注入控制器
.controller('domainController', ['$scope', '$state', 'DomainNameService', function($scope, $state, DomainNameService) {
$scope.addSubTree = function(val){
$scope.DomainNameService = DomainNameService;
}