2017-03-18 106 views
1

这里是关注代码:

<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参数的值是当前页面。 我们在哪里错了?

回答

2

你不能这样做,在HTML,或者你可以获得当前的位置是这样,

分配CURREN位置的范围变量,

$scope.a = window.location.href; 
在HTML

然后,

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

DEMO

<!DOCTYPE html> 
 
<html> 
 

 
<head> 
 
    <script data-require="[email protected]*" 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>

1

其存在$scope可变那些的特性仅适用于上使用HTML作为绑定。所以$location你必须公开的服务$scope

//inject $location to controller before use it. 
$scope.$location = $location;