【问题标题】:ng-click not setting $location.path() when path is dynamic当路径是动态的时,ng-click 不设置 $location.path()
【发布时间】:2017-01-02 03:29:01
【问题描述】:

我正在尝试使用这样的标签:

<a ng-click="$location.path('/restaurant/{{restaurant._id}}')">{{restaurant.name}}</a>

但是,当我点击标签时,什么都没有发生。

奇怪的是,如果我在那里对值进行硬编码,就像这样:

<a ng-click="$location.path('/restaurant/512ad624b67fe1f446709331')">{{restaurant.name}}</a>

它按预期工作。

DOM 的屏幕截图:

为什么会这样?我该如何解决这个问题?

【问题讨论】:

  • 你能做一个 JsFiddle 来演示这个吗?
  • 你可以试试$location.path('/restaurant/' + restaurant.id)吗?见stackoverflow.com/questions/14596235/…
  • 哦!这样可行!如果您想作为问题回答,我会接受。谢谢。
  • 不是“作为问题回答”——但你知道我的意思。

标签: angularjs


【解决方案1】:

AngularJS ng-click not invoked with {{$index}} used,您可以直接使用变量,无需大括号。

<a ng-click="$location.path('/restaurant/' + restaurant._id)">{{restaurant.name}}</a>

【讨论】:

    【解决方案2】:

    希望这会有所帮助!

        $scope.doTheNeedful = function(country){
            var newPath = "home/";
            newPath += country;
            $location.path(newPath);
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-03-05
      • 2016-09-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-24
      • 2014-01-30
      • 1970-01-01
      相关资源
      最近更新 更多