【问题标题】:Angular equivalent of .done in jqueryjquery 中 .done 的 Angular 等价物
【发布时间】:2013-09-16 07:09:01
【问题描述】:

我找不到替代解决方案,可以说我在 jquery 中有这段代码:

$.get 'file.json', (re) ->
   for k, v in re
      tpl = "<div>{{v.content}}</div>";
      $ '#container'.append tpl
 .done () ->
     impress().init()

这很好用,因为.done 仅在 ajax 之后执行代码,但 angular 似乎没有 .done 之类的,而 impress().init() 在加载内容时无法重新初始化,因此会有一个数据绑定错误..

这是我对角度的尝试

App.controller 'SomeCtrl', ($scope, $http) ->
   $http.get('file.json')
   .success (res) ->
      $scope.slides = res
   #what could possibly be in here

【问题讨论】:

    标签: javascript jquery angularjs coffeescript


    【解决方案1】:

    查看similar question,在答案之一Benjamin Gruenbaum suggests 中使用.always,它在所有承诺中提供(包括来自$http.get 的返回值)。他包括一个有用的code example on fiddle

    HTML:

    <div ng-controller="Ctrl">
      {{data}}
    </div>
    

    JavaScript:

    var myApp = angular.module('myApp', []);
    var data = {hello:"World!"};
    function Ctrl($scope, $http) {
        $http.post('/echo/json/', 'json=' + encodeURIComponent(angular.toJson(data)), {
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
            }
        }).always(function(data) {
             alert("HI");  
        });
    }
    

    Benjamin 指出.always 已被.finally 取代。 Here's a fiddle 使用 .finally 和最新的 Angular.js。

    HTML:

    <div ng-controller="MyCtrl">Hello, {{name}}!</div>
    

    JavaScript:

    var myApp = angular.module('myApp', []);
    
    myApp.controller('MyCtrl', ['$scope', '$http', function ($scope, $http) {
        $scope.name = 'Superhero';
        $http.get('/echo/json/').
        success(function() {
            $scope.name = 'Sidekick';
            }).  
        finally(function() {
             alert($scope.name);  
        });
    }]);
    

    (注意这里是the documentationnfiniteloop's answer$q 'finally' not working in IE8 也可能有用。)

    【讨论】:

      【解决方案2】:

      你可以在success之后调用then

      $http.get('file.json')
        .success(function(data) {
          console.log('success');
        })
        .then(function() {
          console.log('success again');
        });
      

      这是example

      【讨论】:

      • 但是如果结果是error 而不是success 怎么办? jQuery 的done 很有用,因为它会在任何一种情况下运行。 Angular.js 是否包含类似的东西?
      • @dumbledad 从 Angular 1.2 开始,您可以使用 finally 捕获成功或错误。
      【解决方案3】:

      Angularjs 有 successerror 的方法。阅读documentation

       $http({method: 'GET', url: '/someUrl'}).
        success(function(data, status, headers, config) {
          // this callback will be called asynchronously
          // when the response is available
        }).
        error(function(data, status, headers, config) {
          // called asynchronously if an error occurs
          // or server returns response with an error status.
        });
      

      【讨论】:

      • jQuery 也有这两个。 done 很有用,因为它提供了一个放置成功或错误后将运行的代码的位置。
      猜你喜欢
      • 1970-01-01
      • 2013-03-02
      • 2021-10-04
      • 2016-05-23
      • 1970-01-01
      • 2011-05-11
      • 2016-04-19
      • 1970-01-01
      相关资源
      最近更新 更多