Javascript 我的范围变量赢得';t在html视图中更新
以下是我的angularjs代码:Javascript 我的范围变量赢得';t在html视图中更新,javascript,html,angularjs,angularjs-scope,Javascript,Html,Angularjs,Angularjs Scope,以下是我的angularjs代码: $scope.attachments = []; $scope.uploadFile = function(files){ for(var i=0; i<files.length; i++){ $scope.attachments.push(files[i]); console.log($scope.attachments.length); } }; <br/>Attachments:
$scope.attachments = [];
$scope.uploadFile = function(files){
for(var i=0; i<files.length; i++){
$scope.attachments.push(files[i]);
console.log($scope.attachments.length);
}
};
<br/>Attachments: {{attachments.length}} {{dummy}}
<ul>
<li ng-repeat="attachment in attachments">
attachment: {{attachment.name}}
</li>
</ul>
</body>
$scope.attachments=[];
$scope.uploadFile=函数(文件){
对于(var i=0;i请这样尝试:
$scope.uploadFile = function(files){
$scope.$apply(function(){
for(var i=0; i<files.length; i++){
$scope.attachments.push(files[i]);
console.log($scope.attachments.length);
}
});
};
<br/>Attachments: {{attachments.length}} {{dummy}}
<ul>
<li ng-repeat="attachment in attachments">
attachment: {{attachment.name}}
</li>
</ul>
</body>
$scope.uploadFile=函数(文件){
$scope.$apply(函数(){
对于(var i=0;iKr:
<br/>Attachments: {{attachments.length}} {{dummy}}
<ul>
<li ng-repeat="attachment in attachments">
attachment: {{attachment.name}}
</li>
</ul>
</body>
HTML:
<br/>Attachments: {{attachments.length}} {{dummy}}
<ul>
<li ng-repeat="attachment in attachments">
attachment: {{attachment.name}}
</li>
</ul>
</body>
附件:{{Attachments.length}}{{dummy}}
-
附件:{{attachment.name}
JS:
<br/>Attachments: {{attachments.length}} {{dummy}}
<ul>
<li ng-repeat="attachment in attachments">
attachment: {{attachment.name}}
</li>
</ul>
</body>
angular.module('app', []).controller('AppCtrl', function($scope) {
$scope.attachments = [];
$scope.uploadFile = function(element) {
$scope.$apply(function(scope) {
for (var i = 0; i < element.files.length; i++) {
scope.attachments.push(element.files[i]);
}
})
};
})
angular.module('app',[]).controller('AppCtrl',function($scope){
$scope.attachments=[];
$scope.uploadFile=函数(元素){
$scope.$apply(函数(范围){
对于(var i=0;i
原始讨论:
<br/>Attachments: {{attachments.length}} {{dummy}}
<ul>
<li ng-repeat="attachment in attachments">
attachment: {{attachment.name}}
</li>
</ul>
</body>
$scope.$apply是因为onchange事件而使用的。但是,我不太清楚为什么ng更改在这种情况下不起作用?为了更清楚,这里需要使用$apply
,因为$scope
由于使用onchange属性而在angular的上下文之外被更改。请参阅讨论:谢谢,这个答案很好,谢谢你对伊瓦尼的解释
<br/>Attachments: {{attachments.length}} {{dummy}}
<ul>
<li ng-repeat="attachment in attachments">
attachment: {{attachment.name}}
</li>
</ul>
</body>