2017-08-08 70 views
-2

我正在開發web應用程序AngularJS。我有下面的表格。

<form name="form2" novalidate><multiselect class="input-xlarge" multiple="true" 
ng-model="selectedCar" options="c.name for c in cars" change="selected()"></multiselect> 
<input type="file" file-modelsr = "myFileDL" /> 
<input type="file" file-modelsr="myFileID" /> 
<input type="submit" value="{{ 'NEXT' | translate }}" class="blue-button" ng-click="saveDetail()"> 

下面是我的JS代碼。

var myapp = angular.module('RoslpApp'); 
(function() { 
    angular.module('RoslpApp').controller('CarOutside', ['$scope', '$http', '$translatePartialLoader', '$translate', '$state', '$stateParams', 'cfg', 'toastr', '$cookieStore', '$filter', '$anchorScroll', '$location', 'fileUpload', 
     function ($scope, $http, $translatePartialLoader, $translate, $state, $stateParams, cfg, toastr, $cookieStore, $filter, $anchorScroll, $location, fileUpload) { 
      $translatePartialLoader.addPart('ServiceRequest'); 
      $translate.refresh(); 
      var cookiePreferredLanguage = $cookieStore.get('PreferredLanguage'); 
      var LoginID = $cookieStore.get("LoginID"); 
      var baseurl = cfg.Baseurl; 
      $translate.use((cookiePreferredLanguage != null && cookiePreferredLanguage != undefined && cookiePreferredLanguage != "") ? cookiePreferredLanguage : 'de_AR'); 
      $scope.name = 'World'; 
      //Bind data to multi select dropdown(location) 
      $scope.cars = [{ id: 1, name: 'Audi' }, { id: 2, name: 'BMW' }, { id: 1, name: 'Honda' }]; 
      $scope.selectedCar = []; 
      //Bind data to period 
      $scope.periodList = [{ id: 1, period: 'Audi' }, { id: 2, period: 'BMW' }, { id: 1, period: 'Honda' }]; 

      if ($scope.form2.$valid) { 
       $scope.saveDetail = function() { 
        var fileDL = $scope.myFileDL; 
        var fileID = $scope.myFileID; 
        var uploadUrl = "/fileUpload"; 
        fileUpload.uploadFileToUrl(file, uploadUrl); 
       }; 
      } else { 
       alert("not valid"); 
      } 
    }]); 
})(); 

我得到下面的錯誤無法讀取屬性undefined$valid和無法弄清楚它爲什麼我收到此錯誤。任何幫助,將不勝感激。謝謝。

+0

請添加更多的JS代碼 – Brian

+0

增加。謝謝。另外我還有兩個指令 –

回答

0
$valid

將您的支票的saveDetail()內。如果($scope.form2.$valid)在您的控制器運行時正在運行,這是$ scope.form2之前有任何數據,因此未定義。

 $scope.saveDetail = function() { 
      if ($scope.form2.$valid) { 
      var fileDL = $scope.myFileDL; 
      var fileID = $scope.myFileID; 
      var uploadUrl = "/fileUpload"; 
      fileUpload.uploadFileToUrl(file, uploadUrl); 
      } else { 
       alert("not valid"); 
      } 
     }; 
0

除了功能saveDetail以外的if.. else,你應該把它們放在裏面。

當前,當它試圖執行$scope.form2.$valid時,表單(form2)尚未初始化,並且是undefined。這就是爲什麼你會得到undefined

$scope.saveDetail = function() { 
    if ($scope.form2.$valid) { 
    var fileDL = $scope.myFileDL; 
    var fileID = $scope.myFileID; 
    var uploadUrl = "/fileUpload"; 
    fileUpload.uploadFileToUrl(file, uploadUrl); 
    } else { 
    alert("not valid"); 
    } 
}; 
0

你應該附上if....else..saveDetail()方法內。

此外,您可以將form2.$valid作爲參數傳遞給ng-submit中的方法,作爲ng-submit="saveDetail(form2.$valid)",並檢查其在方法體內的真實性。

var app = angular.module("app", []); 
 
app.controller("ctrl", function($scope) { 
 
    $scope.saveDetail = function(valid) { 
 
    if (valid) { 
 
     console.log("valid"); 
 
    } else { 
 
     console.log("Not Valid") 
 
    } 
 
    } 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> 
 

 
<body ng-app="app" ng-controller="ctrl"> 
 
    <form name="form2" novalidate ng-submit="saveDetail(form2.$valid)"> 
 
    <multiselect class="input-xlarge" multiple="true" ng-model="selectedCar" options="c.name for c in cars" change="selected()"></multiselect> 
 
    <input type="file" file-modelsr="myFileDL" /> 
 
    <input type="file" file-modelsr="myFileID" /> 
 
    <input type="submit" value="{{ 'NEXT'}}" class="blue-button"> 
 
    </form> 
 
</body>

+0

謝謝....... –

相關問題