简体   繁体   English

无法弄清楚如何使用ui-route

[英]cannot figure out how to use ui-route

I cannot figure out how to correctly use ui-route , the samples on the angular-ui web site do not help. 我无法弄清楚如何正确使用ui-route ,angular-ui网站上的示例无济于事。

Here is my code. 这是我的代码。 I want to implement a simple navigation menu. 我想实现一个简单的导航菜单。 When the route matches that of the item then I want to show the <span> otherwise I want to show <a> . 当路线与该项目的路线匹配时,我想显示<span>否则我想显示<a> I expected $uiRoute to reflect the current route, but apparently it does not because I get <span> elements in the output no matter what the route is. 我希望$uiRoute能够反映当前路由,但是显然这并不是因为无论路由是什么,我都会在输出中得到<span>元素。

http://jsbin.com/ugefoq/2/edit http://jsbin.com/ugefoq/2/edit

<!DOCTYPE html>
<html ng-app="app">
  <head>
    <meta charset=utf-8 />
    <script src="//cdnjs.cloudflare.com/ajax/libs/angular.js/1.1.1/angular.min.js" type="text/javascript"></script>
    <script src="//cdnjs.cloudflare.com/ajax/libs/angular-ui/0.4.0/angular-ui.min.js" type="text/javascript"></script>
    <script type="text/javascript">
      angular.module('app', ['ng', 'ui']);
    </script>
  </head>
  <body>
    <ul>
      <li ui-route="#/link0">
        <a ui-if="$uiRoute" ng-href="#/link0">link0</a>
        <span ui-if="!$uiRoute">link0</span>
      </li>
      <li ui-route="#/link1">
        <a ui-if="$uiRoute" ng-href="#/link1">link1</a>
        <span ui-if="!$uiRoute">link1</span>
      </li>
    </ul>
  </body>
</html>

It looks like you have it right, except that based on your description, you have your ui-if statements reversed. 看起来您做对了,只是根据您的描述,您的ui-if语句已反转。 Right now, your saying, show the link if the url matches my href, show the span if they don't. 您现在说的是,如果url与我的href匹配,则显示链接,如果不匹配,则显示范围。

On another note, if you are doing a single page application here, you may not want to use ui-if as it completely removes the element, meaning you cannot dynamically show it later when your route changes. 另外,如果您在此处执行单个页面应用程序,则可能不希望使用ui-if因为它会完全删除该元素,这意味着您以后无法在路由更改时动态显示它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM