Angularjs将参数从ng-click传递到页面

J. *_*son 8 javascript jquery angularjs angularjs-scope

我的页面顶部有三个按钮,下方有输入框.

<div>
  <form>
          <div>
              Enter Show Name<input type="text" ng-model="showName" />
          </div>
      </form>
</div>
<div>
<button ng-click="href="/api/renameShow"">Rename Show</button>
<button ng-click="href="/api/updateShow"">Update  Show</button>
<button ng-click="href="/api/checkShow"">Check   Show</button>
</div>
Run Code Online (Sandbox Code Playgroud)

带路由的模块代码是

    var showApp = angular.module("showApp", ["ngRoute", "ngResource", "ui"]).
    config(function ($routeProvider, $locationProvider) {
        $routeProvider.
            when('/',
            {
                controller: '',
                templateUrl: 'main.html'
            }).
            when('/api/renameShow', { controller: 'renameShowCtrl', templateUrl:     '/templates/renameShow.html' }).
            when('/api/updateShow', { controller: 'updateShowCtrl', templateUrl:     '/templates/updateShow.html' }).
when('/api/checkShow', { controller: 'checkShowCtrl', templateUrl: '/templates/checkShow.html' });
Run Code Online (Sandbox Code Playgroud)

基本上我要做的是,当单击其中一个按钮时,ng-click调用相应的页面传递参数"showName".请让我知道如何解决它.谢谢

Moh*_*and 14

首先,当我们导航到该页面时,您需要告诉您的目标控制器(您所指的页面)以及接受参数:

$routeProvider.when('/api/renameShow/:showName?', { 
  controller: 'renameShowCtrl', 
  templateUrl:     '/templates/renameShow.html' 
})
Run Code Online (Sandbox Code Playgroud)

参数后面的问号表示它是可选参数.您可以使用锚标记实现相同的目标:

<a href="#/view2/mike">Go to view 2</a>

如果你坚持使用按钮,写一个自定义函数挂钩按钮的ng-click,然后从你当前的控制器传递你想要的任何参数:

<button ng-click="customNavigate('Mike')">Rename Show</button>
Run Code Online (Sandbox Code Playgroud)

在控制器中:

    $scope.customNavigate=function(msg){
       $location.path("/view2"+msg)
    }
Run Code Online (Sandbox Code Playgroud)

然后在目标控制器中:

app.controller("renameShowCtrl",function($scope,$routeParams){
   $scope.showName=$routeParams.showName
});
Run Code Online (Sandbox Code Playgroud)