我是离子的新手,我想上传图片,因此我使用“ng-file-upload”上传我的图片。 但是只有当我删除“ion-content”时代码才能工作。我的模板是:
<ion-view view-title="IQ Marketplace" ng-controller="SellCtrl">
<ion-content class="padding" >
<div class="list">
<form ng-submit="submitAds()" name="form" novalidate>
<label class="item item-input">
<span class="input-label">Picture</span>
<input type="file" ngf-select ng-model="file" name="file" ngf-pattern="'image/*'"
accept="image/*" >
</label>
<input type="submit" class="button button-block button-positive" value="Save" ng-disabled="form.$invalid">
</form>
</div><!--list-->
</ion-content>
</ion-view>
我的控制器是:
.controller('SellCtrl', function($scope,Upload) {
$scope.submitAds = function() {
console.log("ayman "+ $scope.file)
$scope.upload($scope.file);
};
$scope.upload = function (file) {
Upload.upload({
url: 'http://localhost/IQM/public/ads',
data: {file: file, 'username': $scope.test}
}).then(function (resp) {
console.log('Success ' + resp.config.data.file.name + 'uploaded. Response: ' + resp.data);
}, function (resp) {
console.log('Error status: ' + resp.status);
}, function (evt) {
var progressPercentage = parseInt(100.0 * evt.loaded / evt.total);
console.log('progress: ' + progressPercentage + '% ' + evt.config.data.file.name);
});
};
});
这段代码给了我这个例外:
TypeError: Cannot read property 'name' of undefined
at controllers.js:84
at ionic.bundle.js:23476
at Scope.$eval (ionic.bundle.js:24673)
at Scope.$digest (ionic.bundle.js:24484)
at ionic.bundle.js:24712
at completeOutstandingRequest (ionic.bundle.js:14221)
at ionic.bundle.js:14493
但是当我删除“离子内容”时,上传会成功。 有什么问题?
答案 0 :(得分:4)
我猜你已经介绍了Ionic Framework。
这显然是有角度的Dim strCN As String = My.Settings.InovaConnectionString
Dim cn As New SqlClient.SqlConnection(strCN)
Dim ds As New DataSet
Dim DataAdapt As New SqlClient.SqlDataAdapter("SELECT Code, Name, City FROM CUSTOMERS where Nome_Cliente like 'Carl%' ", cn)
DataAdapt.Fill(ds, "CUSTOMERS")
CUSTOMERSDataGridView.DataSource = ds.Tables("CUSTOMERS")
问题。
您在scope
内定义ng-model="file"
。虽然ion-content
创建了新的ion-content
,但您无法直接检索变量scope
。
请参阅doc,并说明:
请注意,此指令有自己的子范围。
您可以使用file
之类的:
dot notations
然后你可以通过// controller
$scope.data = {}
// html
<ion-content class="padding" >
...
<input ng-model="data.file" />
...
</ion-content>
获得价值。
根本原因在于AngularJS的$scope.data.file
概念。
您可以阅读这些文章以获取更多信息。
答案 1 :(得分:0)
我通过添加$ parent解决了这个问题,因此它变成了ng-model =“$ parent.file”
<ion-view view-title="IQ Marketplace" ng-controller="SellCtrl">
<ion-content class="padding" >
<div class="list">
<form ng-submit="submitAds()" name="form" novalidate>
<label class="item item-input">
<span class="input-label">Picture</span>
<input type="file" ngf-select ng-model="$parent.file" name="file" ngf-pattern="'image/*'"
accept="image/*" >
</label>
<input type="submit" class="button button-block button-positive" value="Save" ng-disabled="form.$invalid">
</form>
</div><!--list-->
</ion-content>
</ion-view>