我想这个标题非常清楚我要问的是什么.我创造了这个小提琴:http://jsfiddle.net/Sourabh_/HB7LU/13142/
在小提琴中,我试图复制一个async场景.这只是一个例子,但在AJAX调用中如果我不使用$scope.$apply()该列表则不会更新.我想知道$scope.$apply()每次进行AJAX调用更新列表时是否安全使用,还是有其他一些我可以使用的机制?
我编写的代码用于复制场景(与小提琴相同):
HTML
<div ng-controller="MyCtrl">
<li ng-repeat="item in items">
{{item.name}}
</li>
<button ng-click="change()">Change</button>
</div>
Run Code Online (Sandbox Code Playgroud)
JS
var myApp = angular.module('myApp',[]);
function MyCtrl($scope) {
$scope.items = [{name : "abc"},{name : "xyz"},{name : "cde"}];
$scope.change = function(){
test(function(testItem){
$scope.items = testItem;
//$scope.$apply();
})
}
function test(callback){
var testItem = [
{name : "mno"},
{name : "pqr"},
{name : "ste"}
];
setTimeout(function(){callback(testItem)},2000);
}
}
Run Code Online (Sandbox Code Playgroud) 我有一个
<input type="file" id="aircraftList" name="aircraftList" file-upload multiple/>
Run Code Online (Sandbox Code Playgroud)
绑定到指令
angular.module("app.directives").directive('fileUpload', function () {
return {
scope: true,
link: function (scope, el, attrs) {
el.bind('change', function (event) {
scope.$emit("fileSelected", { files: event.target.files, field: event.target.name });
});
}
};
});
Run Code Online (Sandbox Code Playgroud)
我在控制器中捕获此事件:
$scope.$on("fileSelected", function (event, args) {
$scope.$apply(function () {
switch (args.field) {
case "aircraftList":
self.attachments.aircraftList = args.files;
break;
default:
break;
}
});
});
Run Code Online (Sandbox Code Playgroud)
出于某种原因,这在Chrome和Firefox中运行良好,但在IE11中失败并出现以下错误:
如果我没有把$ apply,chrome没有更新视图,但IE是.如果我把$ apply,Chrome工作完美且IE中断.
谁知道这里出了什么问题以及为什么会出错?
任何人都有一个简单的指令来自动显示Bootstrap模式?在Bootstrap 3中,他们取消了自动显示模态的功能,因此我无法使用角度ng-if show块.任何帮助都会很棒.