以下是相关代码:

<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>

10-06 03:20