我是 angularjs 的新手,我遇到了麻烦,
I have html像这样:
<section class="content" ng-app="offer">
<div ng-controller="OfferController">
...
<input name="Attachment" type="file" class="file_up" onchange="angular.element(this).scope().change(this)" ng-model="Attachment" />
<span>{{Attachment}}</span> //i can't get it
...
</div>
</section>
and script:
var myApp = angular.module('offer', []);
myApp.controller('OfferController', ['$scope', function ($scope) {
$scope.change = function (element) {
if (element.files.length > 0) {
$scope.Attachment = element.files[0].name;
}
else {
$scope.Attachment = "";
}
console.log($scope.Attachment); // I can get changed value in console
}
}]);
image:
I can在控制台中获取更改值,但我can't当输入值改变时进入html端。提前非常感谢您的帮助。
由于您从 Angular 的摘要周期之外更改模型,因此您必须手动通知框架它必须再次运行观察程序。您可以使用$scope.$apply()
为了这:
$scope.change = function (element) {
if (element.files.length > 0) {
$scope.Attachment = element.files[0].name;
}
else {
$scope.Attachment = "";
}
$scope.$apply();
}
或者更简洁:
$scope.change = function(element) {
$scope.$apply(function() {
$scope.Attachment = element.files.length ? element.files[0].name : '';
});
}
Demo: http://plnkr.co/edit/6quQlHJIvdD3QwtOhHrB?p=preview http://plnkr.co/edit/6quQlHJIvdD3QwtOhHrB?p=preview
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)