javascript ng-click 使用参数更改 angularjs 中的路由

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26532978/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 06:05:25  来源:igfitidea点击:

ng-click change route in angularjs with param

javascriptangularjsangular-ui-routerionic-framework

提问by tarek fellah

I would like to change the route of an angularjs application build with ionic framework, but the route didn't change

我想更改使用 ionic 框架构建的 angularjs 应用程序的路由,但路由没有更改

this is my code of app.js

这是我的app.js代码

angular.module('starter', ['ionic', 'starter.controllers'])
.state('app.annuaire.menuitempage', {
  url: "/menuitempage/:ID",
  views: {
    'menuContent' :{
      templateUrl: "templates/menuItemPage.html",
      controller: function($stateParams){
      $stateParams.ID  ;
     }
    }
  }
})

.state('app.annuaire', {
  url: "/annuaire",
  views: {
    'menuContent' :{
      templateUrl: "templates/annuaire.html",
      controller: 'MenuItemCtrl'
    }
  }
})   

And this is the code of my controller

这是我的控制器的代码

  angular.module('starter.controllers', [])
  .controller('MenuItemCtrl', function($scope, $http, $location) {
    $scope.itemsMenu = {};

            var responsePromise =   $http.get("http://monguidepratique.com/mobile/getCategories.php?parent_id=0");

            responsePromise.success(function(data, status, headers, config) {
                //alert(data);
                $scope.itemsMenu = data;
            });
            responsePromise.error(function(data, status, headers, config) {
                alert("AJAX failed!");
            });
   $scope.itemClick = function(path){
            alert(1);
            $location.path(path); 

            };  

   }) 

And this is my html code in annuaire.html

这是我在 annuaire.html 中的 html 代码

 <div class="col"  ng-click="itemClick('/menuitempage/1628')"><img class="img_menu" src="img/home.png"><p class="titre_center">Accueil</p></div>

回答by ababashka

Try

尝试

$location.path(path)

instead of

代替

$state.go(path)

You need to inject $locationservice into your controller.

您需要将$location服务注入控制器。

Edit

编辑

If you are using $state.go- you should to use it next way:

如果您正在使用$state.go- 您应该在接下来的方式中使用它:

$scope.itemClick = function(id){
  $state.go('app.annuaire.menuitempage', {'ID': id})
}; 

And HTML:

和 HTML:

<div class="col"  ng-click="itemClick(1628)"><img class="img_menu" src="img/home.png"><p class="titre_center">Accueil</p></div>

The first param is state name, not URL, the second is an Object with your params.

第一个参数是状态名称,而不是 URL,第二个参数是带有参数的对象。

回答by tarek fellah

I solved my problem

我解决了我的问题

in annuaire.html i changed

在 annuaire.html 我改变了

itemClick('/menuitempage/1628')

by

经过

itemClick('/app/menuitempage/1628') 

and i changed the route name app.annuaire.menuitempage by

我改变了路线名称 app.annuaire.menuitempage

app.menuitempage 

.state('app.menuitempage', {
  url: "/menuitempage/:ID",
  views: {
    'menuContent' :{
      templateUrl: "templates/menuitempage.html",
      controller: 'SubMenuCtrl'
    }
  }
})