为什么 $location.absUrl(), $location.$$url, $location.$$path 在 href 中是空白的?

Why is $location.absUrl(), $location.$$url, $location.$$path blank in href?

关注的代码如下:

<div ng-app="">
https://www.facebook.com/sharer/sharer.php?u={{$location.absUrl()}}
https://www.facebook.com/sharer/sharer.php?u={{$location.$$url}}
https://www.facebook.com/sharer/sharer.php?u={{$location.$$path}}
</div>

我们需要 u 参数的值为当前页面。 我们哪里错了?

你不能在 HTML 中这样做,或者你可以像这样获取当前位置,

将当前位置分配给范围变量,

  $scope.a = window.location.href;

然后在HTML,

   <br>Link = <a ng-href="https://www.facebook.com/sharer/sharer.php?u={{a }}" target="_blank">facebook</a>

演示

<!DOCTYPE html>
<html>

<head>
  <script data-require="angular.js@*" data-semver="1.3.0-beta.5" src="https://code.angularjs.org/1.3.0-beta.5/angular.js"></script>
  <link rel="stylesheet" href="style.css" />
  <script src="script.js"></script>
  <script type="text/javascript">
    var myApp = angular.module('myApp', []);

    myApp.controller('TestCtrl', ['$scope', function($scope) {
      $scope.a = window.location.href;
    }]);
  </script>
</head>

<body ng-app="myApp">

  <div ng-controller="TestCtrl">
     <input type="text" ng-model="a">
    <br>a={{a}}
    <br>
    <br>Link = <a ng-href="https://www.facebook.com/sharer/sharer.php?u={{a}}" target="_blank">facebook</a>
  </div>
</body>

</html>

$scope 变量中存在的 属性 只能在 HTML 上用作绑定。所以 $location 您必须在 $scope

上公开服务
//inject $location to controller before use it.
$scope.$location = $location;