angularjs,使用函数在路由作为templateUrl [英] angularjs, use a function in the routing as the templateUrl
问题描述
这是可以使用函数作为templateUrl。为$ routeProvider官方文档声明:
templateUrl - {字符串= |函数()=}
块引用>在JavaScript中,一个函数的语法:
函数(){code}在这里
但文档显示:
函数()=
使用
函数()= code以下
不起作用。哪些是angularjs路由使用的templateUrl一个函数的语法?如果我用的是这样的:
时,('/存储/:STOREID',{
templateUrl:函数()=网页/ Stores.php STOREID ='+:STOREID,
控制器:'storeParseData这是行不通的。我想这对
的URL匹配:STOREID
自动存储在$ routeProvider
。我已经使用$范围
,$位置
试过了,$ routeProvider
在没有成功的各种不同的方式。我在寻找一个URL#散列ID传递给templateUrl最简单,最直接的方式。我需要注入动态信息到templateUrl究其原因,是因为我在页面加载是加载时运行PHP code一
PHP
文件。在PHP
code检索哈希#URL信息。检索:在控制器STOREID
不会帮助我。我想在PHP
文件从服务器端
,而不是前端检索数据
JavaScript的控制器。下面是PHP code:
< PHP
//有两种基本方案。 1)获取所有商店的名称来填充店铺列表
// 2)获取所有的帖子从一个特定的用户,(存储)
//如果URL有'猫=什么',然后让所有的存储数据
//如果URL没有'猫=什么',然后获取存储数据。//只拿到店里的名字,如果DIV与商店的名称是空的,否则
//刚刚拿到专卖店上市。$ storeNames =的file_get_contents('https://NameOfDataURLHere.com/.json');//这是previously投入URL字符串结尾信息被分析有以下code。
$ urlPiece = $ _ SERVER ['REQUEST_URI'];
回声(URL海贼王:$ urlPiece);
//发现在URL字符串搜索标准的第一个字符的位置:STOREID ='。
//首先,URL被检查'STOREID ='。如果'STOREID ='在URL退出,那么页路由注入'STOREID ='
//作为一个参数字符串传递。如果没有参数传递,然后'STOREID ='字符串。
$ posOfEqualSign = strrpos($ urlPieceSTOREID =);
回声(< BR>中);
回声(posOfEqualSign:$ posOfEqualSign);
回声(< BR>中);
//如果找到字符串STOREID =',没关系继续。如果(使用isset($ posOfEqualSign)及&放大器; strlen的($ posOfEqualSign)大于0){
//如果你想默认要去字符串的结尾在PHP SUBSTR函数并不需要一个长名称。
$ STOREID = SUBSTR($ urlPiece,$ posOfEqualSign + 8);
回声(STOREID长:.strlen($ STOREID));
回声(< BR>中);
回声(STOREID:$ STOREID);
回声(< BR>中);
}如果(使用isset($ STOREID)及&放大器; strlen的($ STOREID)→3){
//获取店里的所有目录出来的房源数据库与存储ID
//商店ID从URL检索。当用户点击店名商店ID投入的URL。
$ storeListings =的file_get_contents('https://NameOfDataURLHere.com/'.$storeID.'/.json');
$ listingsDe codeD = json_de code($ storeListings,真正的); 回声(storeListings:$ storeListings);
回声(< BR>中); 如果(strlen的($ storeListings)→1){
的foreach($ listingsDe codeD为$关键=> $值){
。// $ keyValHere =\\$键\\; //回声keyValHere。 $ keyValHere;
回声< BR>< BR>中;
回声等级1键:。 $关键;
呼应'< BR>< BR>'; $ storeListingsData =阵列(); 的foreach($值$ X => $ V){
回声二级重点。 $ X;
回声< BR>中;
回声Lelvel 2值:。 $ V;
回声< BR>中;
$ specificListing = SUBSTR($ V,4);
$ catToGetItemFrom = SUBSTR($ V,0,3);
回声上市编号:'。 $ specificListing;
回声< BR>中;
回声'类别:'。 $ catToGetItemFrom;
如果(strlen的($ catToGetItemFrom)大于0&放大器;&放大器; strlen的($ specificListing)→1){
如果(ORD($ catToGetItemFrom)LT = 109){// ORD返回一个字符串的第一个字符的ASCII值
$ whatDB =NameOfDBHere;
}其他{
$ whatDB =NameOfSecondDBHere;
}
$ oneStoreListing = file_get_contents('https://'.$whatDB.'.dataURL.com/'.$catToGetItemFrom.'/'.$specificListing.'/.json');
}其他{
$ oneStoreListing =;
回声(一店清单:'$ oneStoreListing);
} 如果(strlen的($ oneStoreListing)→1){
//保存,直到循环完成增加单店上市到阵列。
array_push($ storeListingsData,$ oneStoreListing);
}
}
}
}呼应'< BR>';
回声(商店名称:'。$ storeNames);
回声('< BR>');
回声(一店清单:'$ storeListingsData);}
?>数据注入DIV用PHP为以后的检索:
< BR>< DIV ID =idCatName的风格=显示:内联?>< PHP的echo $ catToGetItemFrom;>< / DIV>
< BR>
< DIV ID =idStoreNames的风格=?显示:内联>< PHP的echo($ storeNames);>< / DIV>
< BR>
< DIV ID =idStoreData的风格=?显示:内联>< PHP的echo($ storeListingsData);>< / DIV>解决方案 - 与一些帮助从其他:
myApp.config(['$ routeProvider',
功能($ routeProvider){$ routeProvider。
当('/存储/:STOREID',{
templateUrl:功能(PARAMS){回报?网页/ Stores.php STOREID ='+ params.StoreId;},
控制器:'storeParseData
})。
否则({redirectTo:'/家庭'});
}]);
解决方案。当('/:STOREID /私',{
templateUrl:功能(PARAMS){回报网页/店'+ params.storeId;},
控制器:'storeParseData
})It's possible to use a function as the templateUrl. The official documentation for $routeProvider states:
templateUrl – {string=|function()=}
$routeProvider official documentation
In javascript, a function has the syntax of:
function() {code here}
But the documentation shows:
function()=
Using
function()=Code Here
doesn't work. What is the syntax for using a function for the templateUrl in angularjs routing?If I use something like:
when('/Store/:StoreId', { templateUrl: function()=webPages/Stores.php?storeID=' + :StoreId, controller: 'storeParseData'
This does not work. I guess that the URL match for
:StoreId
is automatically stored in$routeProvider
. I've tried using$scope
,$location
, and$routeProvider
in various different ways with no success. I'm looking for the simplest and most straight forward way to pass a URL # hash id to the templateUrl.The reason I need to inject dynamic info into the templateUrl, is because the page I'm loading is a
PHP
file that runs PHP code when loaded. ThePHP
code retrieves info from the hash # URL. Retrieving the:StoreId
in the controller won't help me. I want thePHP
file to retrieve data from theserver side
rather than thefront end
JavaScript controller.Here is the PHP code:
<?php //There are two basic scenarios. 1) Get all Store names to populate the Store List // 2) Get all the postings from a particular user, (store) // If the URL has 'cat=something', then get all the store data // If the URL does NOT have 'cat=something', then get the store data. //Only get the store names if DIV with the store names is empty, otherwise //just get the stores listings. $storeNames = file_get_contents('https://NameOfDataURLHere.com/.json'); //information that was previously put into the URL string ending, is parsed with the following code. $urlPiece = $_SERVER['REQUEST_URI']; echo("url Piece: ".$urlPiece); //find the position of the first character in the URL string with search criteria: 'storeID='. //First the URL is checked for 'storeID='. If 'storeID=' exits in the URL, then the page routing injected 'storeID=' //as a string for an argument to be passed. If no argument was passed, then 'storeID=' string. $posOfEqualSign = strrpos($urlPiece,"storeID="); echo("<br>"); echo("posOfEqualSign: ".$posOfEqualSign); echo("<br>"); //If the string 'storeID=' was found, it's okay to proceed. if (isset($posOfEqualSign) && strlen($posOfEqualSign) > 0) { //the substr function in PHP doesn't need an length designation if you want the default to go to the end of the string. $storeID = substr($urlPiece,$posOfEqualSign + 8); echo("storeID Length: ".strlen($storeID)); echo("<br>"); echo("storeID: " . $storeID); echo("<br>"); } if (isset($storeID) && strlen($storeID) > 3) { //get all listings for the store out of the listings database with the store ID //The store ID was retrieved from the URL. The store ID was put into the URL when the user clicked the store name. $storeListings = file_get_contents('https://NameOfDataURLHere.com/'.$storeID.'/.json'); $listingsDecoded=json_decode($storeListings,true); echo("storeListings: ".$storeListings); echo("<br>"); if (strlen($storeListings) > 1) { foreach ($listingsDecoded as $key => $value) { //$keyValHere = "\"" . $key . "\""; //echo "keyValHere " . $keyValHere; echo"<br><br>"; echo "Level 1 Key: " . $key; echo '<br><br>'; $storeListingsData = array(); foreach($value as $x=>$v) { echo "Level 2 Key: " . $x; echo"<br>"; echo "Lelvel 2 value: " . $v; echo"<br>"; $specificListing = substr($v, 4); $catToGetItemFrom = substr($v,0,3); echo 'The Listing ID: ' . $specificListing; echo"<br>"; echo 'The category: ' . $catToGetItemFrom; if (strlen($catToGetItemFrom) > 0 && strlen($specificListing) > 1) { if (ord($catToGetItemFrom) <= 109) { //ord returns the ASCII value of the first character of a string $whatDB = "NameOfDBHere"; } else { $whatDB = "NameOfSecondDBHere"; } $oneStoreListing = file_get_contents('https://'.$whatDB.'.dataURL.com/'.$catToGetItemFrom.'/'.$specificListing.'/.json'); } else { $oneStoreListing = ""; echo('one store Listing: '.$oneStoreListing); } if (strlen($oneStoreListing) > 1) { //Keep adding single store listings to the array until the loop is done. array_push($storeListingsData, $oneStoreListing); } } } } echo '<br>'; echo('store Names: '.$storeNames); echo('<br>'); echo('one store Listing: '.$storeListingsData); } ?>
Data Injected Into A DIV with PHP for Later Retrieval:
<br> <div id="idCatName" style="display: inline"><?php echo $catToGetItemFrom;?></div> <br> <div id="idStoreNames" style="display: inline"><?php echo($storeNames);?></div> <br> <div id="idStoreData" style="display: inline"><?php echo($storeListingsData);?></div>
The Solution - With a Little Help From Others:
myApp.config(['$routeProvider', function($routeProvider) { $routeProvider. when('/Store/:StoreId', { templateUrl: function(params){return 'webPages/Stores.php?storeID=' + params.StoreId;}, controller: 'storeParseData' }). otherwise({ redirectTo:'/Home' }); }]);
解决方案.when('/:storeId/private', { templateUrl: function(params) {return 'webPages/Stores' + params.storeId;}, controller: 'storeParseData' })
这篇关于angularjs,使用函数在路由作为templateUrl的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!