Angular ng Click:仅适用于某些情况

时间:2014-12-15 16:42:14

标签: angularjs angular-ui

我正在尝试使用ng-click覆盖<a>链接,但它仅在我使用基本网址加载页面时才有效。

我意识到这听起来很奇怪,但您可以通过将http://afmnew.herokuapp.com/http://afmnew.herokuapp.com/criteria/price/1进行比较并使用右侧的按钮来应用过滤器来查看结果。在后一种情况下,您将看到地图在重新加载时闪烁,而在前者中只是删除并添加标记。

这是代码的关键部分,用HTML表示:

<div class="btn-group btn-group-justified price">
    <a href="criteria/price/1" class="btn btn-default" ng-click="main.applyFilter($event)">Budget</a>
    <a href="criteria/price/2" class="btn btn-default" ng-click="main.applyFilter($event)">Mid-range</a>
    <a href="criteria/price/3" class="btn btn-default" ng-click="main.applyFilter($event)">Fine dining</a>
</div>

这是我控制器中的相关功能。当我使用较长的URL时,控制器肯定会被加载,因为此控制器处理URL以应用相关的过滤器。我现在不知道从哪里开始尝试追踪问题。

angular.module 'afmnewApp'
.controller 'MainCtrl', (Restos, $stateParams, $state, Dictionary) ->

    this.reset = () ->
        console.log "MainCtrl: reset"
        this.data.offcanvas = false
        Restos.reset()

    apply = (q, v) ->
        Restos.data.headTitle = "The Amsterdam Foodie's top "+Restos.data.pageTitle
        Restos.data.metaDescription = "The Amsterdam Foodie's top "+Restos.data.pageTitle
        if v == 'price'
            Restos.data.pageTitle = Dictionary.prices[q-1].name + " "+ Dictionary.prices[q-1].suffix
            Restos.priceFilter parseInt q
        else
            Restos.data.pageTitle = Dictionary.cuisines[q].name + Dictionary.cuisines[q].suffix
            Restos.cuisineFilter q  

    this.applyFilter = (event) ->
        console.log event.target.href
        event.preventDefault()
        params = event.target.href.match(/criteria\/(.*)\/(.*)$/)
        console.log("MainCtrl: applyFilter: ", params) if Restos.debug
        this.data.offcanvas = false
        # $state.transitionTo 'criteria', {criteria:params[1], q:params[2]}, {inherit:false}
        # stateObj =
        #    criteria: params[1]
        # history.pushState({}, "null", "criteria/"+params[1]+"/"+params[2]);
        apply params[2], params[1]

    this.data = Restos.data     # needed to pass click from navbar
    # preparePage = () ->
    console.log "MainCtrl: loading..."
    # Restos.data.offcanvas = false
    # This code causes the problem
    if $stateParams.criteria
        q = $stateParams.q
        ga 'send', 'pageview', '/'+$stateParams.criteria+'/' + q
        # Restos.data.intro = false
        # console.log "mainCtrl: Criteria type %s, value %s", $stateParams.criteria, q if Restos.debug
        apply $stateParams.q, $stateParams.criteria
    else
        ga 'send', 'pageview', '/'
        Restos.data.pageTitle = "Restaurant recommendations"
        Restos.data.headTitle = "Restaurant recommendations by the Amsterdam Foodie"
        Restos.data.metaDescription = "The Amsterdam Foodie's top restaurant recommendations from among 400 reviews"
        # default filter is applied anyway

1 个答案:

答案 0 :(得分:0)

尝试使用event代替$event

 angular.module('afmnewApp').controller('MainCtrl', function() {
    this.applyFilter = function(event) {
      event.preventDefault();
      // change model and thus view...
    };