我试图使用AngularJS访问PHP变量。下面是我的HTML和Angular代码:
<!DOCTYPE html>
<html>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<body>
<div ng-app="myApp" ng-controller="mainCtrl">
Your Link: <div> <span ng-bind="fetchLink.link"></span></div>
</div>
<script>
angular.module('myApp', [])
.controller('mainCtrl', ['$http', function($http) {
var self = this;
self.link = "";
self.newTodo = {}; //Keys (as in key: value pairs) are automatically created when declared in form
var fetchLink = function() {
return $http.get('link.php').then(
function(response) {
self.link = response.data;
}, function(errResponse) {
console.error('Error while fetching notes');
});
};
}]);
</script>
</body>
</html>
这是我的PHP:
<?php
{ $link = 0x00;
"link":[{$link}];
$link= ($link + 1);
}
?>
我正在尝试使用Angular代码来访问'link'变量,该变量必须在任何用户每次访问时更新(十六进制值增加1)。这样每个用户都可以获得一个唯一的链接,他们可以访问并与他们的朋友分享。当我预览html页面,它只是说你的链接:没有任何值旁边。我还考虑过使用JSON对象,我相信它可以很好地与Angular配合使用,但我不知道JSON对象是否可以在每次使用时更新(因为它们是客户端对象,而不是服务器端)。
<?php
$link = "http://example.com/".time();
echo json_encode(array("link"=>$link));
?>
使用time()和rand()在json中使用json_encode()函数创建唯一链接并返回链接