0

编辑:我在进行更改时更新代码块以及底部的错误

所以我在我的指令内。我想刷新页面。(就像滚动刷新后一样)

什么是正确的角度方式来做到这一点,角度?(我可以用 jquery 破解它,但我正在尝试以有角度的方式做事)

我的计划是获取我目前所在的网址。然后设置 $location.path(my current url);

它不起作用,因为我似乎无法弄清楚我当前的网址是什么。

// USAGE: <div alert-bar alertMessage="myMessageVar"></div>
angular.module('foo.directives').
directive('iScroll', ['$rootScope', '$parse', '$location', function($rootScope, $parse,    $location) {
   return {
   restrict: 'C',

link: function(scope, elem, attrs) {
  console.log("iscroll!");

 //document.addEventListener('touchmove', function (e) { e.preventDefault(); }, false);

 new iScroll('wrapper', { useTransition: false, bounce:true, vScroll:true,
  onScrollEnd: function ($location) {
    console.log("onScrollEnd");
    //x=$location.path();


      x=$location.url();
    console.log("url is "+x);

  },
} );
   }
}
}]);

现在产量

未捕获的类型错误:无法调用未定义的方法“url”

       // USAGE: <div alert-bar alertMessage="myMessageVar"></div>
        angular.module('foo.directives').
       directive('iScroll', ['$rootScope', '$parse', '$location', function($rootScope, $parse,    $location) {
   return {
   restrict: 'C',

link: function(scope, elem, attrs) {
  console.log("iscroll!");

 //document.addEventListener('touchmove', function (e) { e.preventDefault(); }, false);

 new iScroll('wrapper', { useTransition: false, bounce:true, vScroll:true,
  onScrollEnd: function ($location) {
    console.log("onScrollEnd");
    //x=$location.path();


      x=$location.path();
    console.log("path is "+x);

  },
} );
   }
}
}]);

现在产量

未捕获的类型错误:无法调用未定义的方法“路径”

4

2 回答 2

2

在你的指令定义中,$location 被注入到 $parse 和 $parse 到 $location。

您日志中的对象是 $parse (来自角度来源):

$parse = function(exp) {
  switch(typeof exp) {
    case 'string':
      return cache.hasOwnProperty(exp)
        ? cache[exp]
        : cache[exp] =  parser(exp, false, $filter, $sniffer.csp);
    case 'function':
      return exp;
    default:
      return noop;
  }
};

这应该工作:

angular.module('foo.directives').directive('iScroll', ['$rootScope', '$parse', '$location', function($rootScope, $parse, $location) {
  return {
    restrict: 'C',
    link: function(scope, elem, attrs) {
      new iScroll('wrapper', { useTransition: false, bounce:true, vScroll:true,
        onScrollEnd: function () {
          console.log("onScrollEnd");
          console.log("url is " + $location.url());
        },
      });
    }
  }
}]);
于 2013-07-30T19:59:13.710 回答
0

所以事实证明我想出了如何做到这一点。获取您当前的位置然后将 location.path() 设置到同一个位置是非常糟糕的刷新。

我所做的是在我的一个控制器中编写一个刷新功能

    $scope.refresh = function() {

  return Headlines.get(queryId).then( function( headlines ) {
   // modify the view of the page you wanted to refresh here by updating your data 
  });

然后修改我的指令以调用该刷新函数。注意:您需要在刷新函数周围使用 scope.$apply(function() { } 否则它只会在 iscroll 完成操作页面后发生。

angular.module('foo.directives').directive('iScroll', ['$rootScope', '$parse', '$location', function($rootScope, $parse, $location) {
  return {
  restrict: 'C',

link: function(scope, elem, attrs) {
  console.log("iscroll!");

  document.addEventListener('touchmove', function (e) { e.preventDefault(); }, false);

  new iScroll('wrapper', { useTransition: false, bounce:true, vScroll:true,
    onScrollEnd: function ($location) {
      console.log("onScrollEnd");


   scope.$apply(function() {
     scope.refresh();
   });

     },
    });
   }
  }        
}]);
于 2013-07-30T21:36:18.237 回答