2017-10-04 74 views
1

我無法找到具有現有答案的解決方案,因此我正在創建新帖子。表單驗證到ng-click指令

我想添加驗證按鈕點擊。

我的情況是這樣,

我有兩個輸入框和一個圖像上載一個形式,在這裏我們有默認的圖像和用戶還可以上傳一個新的機會。 如果用戶未上傳圖片,我們必須在點擊提交後顯示錯誤消息。

<div class="form-group"> 
      <img src="{{ImageUrl}}" class="img-responsive m-b-md" /> 
      <button type="button" class="btn btn-primary-outline btn-xs pull-right dash-post-button" aria-label="Post" ng-click="changeImage()"> 
      <span class="glyphicon glyphicon-pencil" aria-hidden="true"></span> 
      <small>change image</small> 
      </button> 
      <span ng-show="errImg">please choose the image</span> 
     </div> 

的上傳圖片:

$scope.iSupportImageUrl = "images/iceland.jpg"; 


     var setiSupportImageUrl = function (iSupportImgId) { 
      $scope.iSupportImageUrl = AWSBucketNameConverter.getAWSURL('TELEKHA_ISUPPORT_IMG', iSupportImgId); 

     }; 


     $scope.changeiSupportImage = function() { 
     var iSupportImgUpldmodalInstance = $modal.open({ 
      controller: 'EventimageuploadCtrl', 
      templateUrl: 'views/isupportimageupload.html', 
      animation: true 
     }); 

     iSupportImgUpldmodalInstance.result.then(
      function (result) { 
      setiSupportImageUrl(result.fileId); 
      result.status='a'; 
      result.createdTS='2014-05-04'; 
      result.containerName='TELEKHA_ISUPPORT_IMG'; 
      result.createdById=23; 
      result.createdName='abc'; 
      $scope.newiSupport.bannerImage=result 
      }, 
      function() { 

      } 
     ); 
     }; 
+0

所以現在有什麼問題?你有什麼內部changeImage – Sajeetharan

+0

@Sajeetharan當我點擊提交它應該顯示錯誤消息,但現在它提交。 – Ajay

+0

只是返回,如果有錯誤 – Sajeetharan

回答

0

你可以寫你的函數是這樣的:

$scope.changeImage = function(imageModel){ 
    if(imageModel){ 
     // upload the image 
    } 
    else { 
     // Show up the error message 
    } 
    } 
+0

已更新帖子 – Ajay

0

最好的解決辦法是在你的IMG添加一個隱藏的輸入字段。所以你可以使用它的要求。東西沿着這些線

<div 
    layout="column" 
    flex 
    ng-mouseenter="display_icon_picture = true" 
    ng-mouseleave="display_icon_picture = false" 
    layout-align="center center" 
    class="profile-pic-container"> 
    <img ng-if="profile_picture_url" ngf-src="profile_picture_url"> 
    <img ng-if="!!!profile_picture_url" class="profile-picture" ngf-src="default_image"> 
    <md-icon ng-if="display_icon_picture">add_a_photo</md-icon> 
</div> 
<md-input-container class="form-error" id="Photo" style="font-weight:bold;"> 
    <input style="display: none;" type="text" ng-model="profile_picture_url" name="Photo" required aria-label="input"> 
    <div ng-messages="form['Photo'].$error" md-auto-hide="false"> 
     <div ng-message="required">Requis</div> 
    </div> 
</md-input-container>