【发布时间】:2014-09-08 11:38:25
【问题描述】:
在用户选择文件后,我正在尝试禁用文件input 标签。
HTML:
<div ng-controller='firstController'>
<div ng-controller='secondController'>
<input type="file" name="file" upload class="theFileInput" ng-disabled="fileInMemory">
</div>
</div>
JS,第一个控制器:
$scope.fileInMemory = false; // tracks if user selected a file for upload, but didn't upload it
$rootScope.$on('fileAdded', function () {
$scope.fileInMemory = true;
console.log($scope.fileInMemory);
});
upload 是一个指令。
在页面加载时,ng-disabled 具有应有的 false,当 fileInMemory 更改时,input 标记仍未被禁用。 console.log 表明fileInMemory 的值正在发生应有的变化。
到目前为止我尝试了什么
<input type="file" name="file" class="theFileInput" ng-disabled="'fileInMemory'">
当fileInMemory 为假时,立即禁用该字段。
<input type="file" name="file" class="theFileInput" ng-disabled="fileInMemory == 'true'">
没用。
<input type="file" name="file" class="theFileInput" ng-disabled="{{fileInMemory}}">
还是不行。
禁用input 标签的最佳方法是什么?
发现问题
看起来这里的问题是范围。
我有 firstController 和它的范围,在它里面有 secondController 和它的范围,在 input 标签 upload 指令上,它显然创建了它自己的范围并且看不到 firstController 范围。
secondController 和 upload 是通用控制器,因此不继承自 firstController。
我想我最好的解决方案是在secondController 中添加一些通用处理程序,基于input 字段上的附加属性将在需要时禁用它。
【问题讨论】:
-
不要使用表达式,只使用变量
-
我认为文件输入不在fileInMemory所属的范围内。你能做一个演示吗?
-
@charlietfl 我也使用了该变量,但它不起作用。
-
@Alborz 我如何检查它们是否在同一范围内?当我将
fileInMemory的值输出到 HTML 时,它显示正确,值正确。 -
您使用嵌套范围吗?使用 abject 而不是原始类型。
标签: javascript html angularjs