2017-06-19 28 views
0

通過點擊「添加行」按鈕,使用AngularJS添加一行到HTML表格,只需執行像$ scope.photos.push({})之類的操作即可。 。新行包含一個文件輸入字段,我想在新行被渲染後觸發文件輸入字段的文件對話框。這是可能的和如何?Angularjs,在渲染後用ng-repeat做一些HTML元素UI

.controller('mainController', function ($scope) { 
    $scope.photos = []; 
    $scope.addPhoto = function(){ 
    $scope.photos.push({}); 
    document.getElementById("photo" + $scope.photos.length-1).click(); // this won't work because the element is not yet rendered. 
    } 
}); 

HTML

<tr ng-repeat="photo in photos"> 
<td> <input type="file" id="photo{{ $index }}"> </td> 
</tr> 
+1

請包括一些代碼。 – 31piy

+0

這是可能的。確保您生成的新行和它的數據存儲在數據庫中。 –

+0

試試這個: - angular.element(document.querySelector('#id'))。click(); –

回答

0

順便說一句,我沒有看到你打電話$scope.$apply();在你的方法,以動態呈現的東西。

+0

錯誤:$ rootScope:inprog 已執行操作 –

+0

我的意思是,在將新照片推送到範圍之後,您需要重新計算範圍。 – Artem

+0

是的,沒錯。這是錯誤發生的地方 –

0

使用$超時如下,它的工作原理:

.controller('mainController', function ($scope, $timeout) { 
    $scope.addPhoto = function(){ 
    $scope.photos.push({}); 
    $timeout(function(){ 
     document.getElementById("photo" + $scope.photos.length-1).click(); // this works now 
    }) 
    } 
});