angularjs,在路由中使用一个函数作为templateUrl [英] angularjs, use a function in the routing as the templateUrl

查看:17
本文介绍了angularjs,在路由中使用一个函数作为templateUrl的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可以使用函数作为 templateUrl.$routeProvider 的官方文档指出:

It's possible to use a function as the templateUrl. The official documentation for $routeProvider states:

templateUrl – {string=|function()=}

templateUrl – {string=|function()=}

$routeProvider 官方文档

在javascript中,函数的语法如下:

In javascript, a function has the syntax of:

function() {code here}

但文档显示:

function()=

使用 function()=Code Here 不起作用.在 angularjs 路由中为 templateUrl 使用函数的语法是什么?

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'

这不起作用.我猜 :StoreId 的 URL 匹配会自动存储在 $routeProvider 中.我曾尝试以各种不同的方式使用 $scope$location$routeProvider,但都没有成功.我正在寻找将 URL # hash id 传递给 templateUrl 的最简单、最直接的方法.

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.

我需要将动态信息注入到 templateUrl 中的原因是我加载的页面是一个 PHP 文件,它在加载时运行 PHP 代码.PHP 代码从哈希 # URL 中检索信息.在控制器中检索 :StoreId 对我没有帮助.我希望 PHP 文件从 server side 而不是 front end JavaScript 控制器检索数据.

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. The PHP code retrieves info from the hash # URL. Retrieving the :StoreId in the controller won't help me. I want the PHP file to retrieve data from the server side rather than the front end JavaScript controller.

<?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);

}
?>

使用 PHP 将数据注入 DIV 以供以后检索:

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

解决方案 - 在他人的帮助下:

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆