当我使用php函数substr($id,1)转义参数冒号(:id)时,我的瘦路由(index.php)只将数据返回到angular(app.js)。
当我注释substr函数时,由于参数id之前的冒号,返回为false。
为什么这样?
app.js(角度)
var app = angular.module('app',['ngRoute']);
app.config(['$routeProvider',function($routeProvider) {
$routeProvider.
when('/',{controller: 'ListCtrl',templateUrl:'partials/list.html'}).
when('/product/:param',{controller:'ListIdCtrl',templateUrl:'partials/list.html'}).
otherwise({redirectTo:'/'});
}]);
app.controller('ListIdCtrl',['$scope','$http','$routeParams', function($scope,$http,$routeParams){
var requestId = $routeParams.param;
console.log(requestId);
$http.get("../product/" + requestId).success(function(data) {
console.log(data); //return false
}).
error(function(){
console.log('Error on get json');
});
}]);
index.php(瘦)
require 'lib/Slim/Slim.php'; //include the framework in the project
'Slim'Slim::registerAutoloader(); //register the autoloader
$app = new 'Slim'Slim(array());
$app->get('/product/:id','getProduct');
$app->run();
function getProduct($id) {
$sql = "select * from wines where id =:id";
try {
$db = getConnection();
$stmt = $db->prepare($sql);
$id = substr($id, 1); //need to use substr to remove colon on :id
$stmt->bindParam(":id", $id, PDO::PARAM_INT);
$stmt->execute();
$wine = $stmt->fetchObject();
$db = null;
echo json_encode($wine);
} catch(PDOException $e) {
echo '{"error":{"text":'. $e->getMessage() .'}}';
}
}
function getConnection() {
return $dbh;
}
我在html href:上发现了问题
之前
<a class="text-link" href="#/product/:{{vinho.id}}" ><h4>{{vinho.username}}</h4></a>
在我使用ng href和您的结肠之后
<a class="text-link" ng-href="#/product/{{vinho.id}}" ><h4>{{vinho.username}}</h4></a>