【问题标题】:Conditionally apply classes depending on route location in Angular JS根据 Angular JS 中的路由位置有条件地应用类
【发布时间】:2013-08-10 23:29:38
【问题描述】:

我的目标是根据 AngularJS 服务的路由向 body 添加一个类。

例如,我想这样做(在 HAML 中):

%body#body{:class => "{{$route.current.templateUrl}}"}

$route 指的是此处文档中的 var:http://docs.angularjs.org/api/ng.$route

这样,如果我访问 /#/whatever,它应该加载一个 whatever.html 模板,并且该模板文件名应该显示为一个类,因此它是 <body class="whatever.html">

【问题讨论】:

    标签: angularjs haml


    【解决方案1】:

    绑定在scope 的上下文中解析。所以绑定属性必须在作用域上可用。 您可以做的是将$route 注入到您的控制器中,并将您想要绑定到控制器$scope 属性的属性分配给它,例如

    $scope.templateUrl=$route.current.templateUrl;
    

    然后绑定到templateUrl

    【讨论】:

      猜你喜欢
      • 2013-10-08
      • 1970-01-01
      • 2020-11-06
      • 2020-05-21
      • 2017-09-29
      • 2019-07-06
      • 1970-01-01
      • 2023-03-25
      • 2016-03-28
      相关资源
      最近更新 更多