2015-05-27 156 views
0

上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 }); 
     } 

有沒有辦法使用服務發送錯誤消息。提前致謝。!

+0

你包括DomainNameService在你的網域控制器在控制器中注入這家工廠,然後?如果是這樣,我們需要看到這個控制器代碼 –

+0

爲什麼在世界上你使用承諾? – sirrocco

回答

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; 
} 
+0

我已經試過你的代碼,但仍然沒有得到HTML頁面上的錯誤信息。請爲此提出解決方案。我在控制器中添加了我的服務 –

+0

您是否在您的控制器中嘗試了$ scope.DomainNameService = DomainNameService; – Vineet

+0

我沒有試過這個。我會試試這個。謝謝vineet :) –

相關問題