相关疑难解决方法(0)

什么时候使用$ scope是安全的.$ apply()?

我想这个标题非常清楚我要问的是什么.我创造了这个小提琴: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)

javascript ajax angularjs

14
推荐指数
3
解决办法
2万
查看次数

Angular:$已在IE11中正在进行中,但不适用于FF和Chrome

我有一个

<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中断.

谁知道这里出了什么问题以及为什么会出错?

file-upload angularjs

7
推荐指数
2
解决办法
1381
查看次数

Bootstrap模态的简单角度指令

任何人都有一个简单的指令来自动显示Bootstrap模式?在Bootstrap 3中,他们取消了自动显示模态的功能,因此我无法使用角度ng-if show块.任何帮助都会很棒.

twitter-bootstrap angularjs

6
推荐指数
2
解决办法
3万
查看次数