Angular 1.6.x: 如何使用ng-click从一个控制器重定向到主页?

Angular 1.6.x: how to redirect from one controller to homepage using ng-click?

我有一个 contact page,那个页面上有一个 home btn。我想使用 ng-click 将它从 contact page 重定向到 home page

下面是各个页面的代码。

contact.html

<div ngController="contactController">
        <div class="heading text-center">
        <h1>Contact Us</h1>
    </div>
    <div>
        <form class="needs-validation" novalidate>
            <div class="form-group row">
                <label for="validationTooltip01" class="col-sm-2 col-form-label">Name</label>
                <div class="input-group">
                    <input type="text" class="form-control" id="validationTooltipName" placeholder="Name" required>
                    <div class="invalid-tooltip">
                        Please enter your full name.
                    </div>
                </div>
            </div>
            <div class="form-group row">
                <label for="validationTooltipEmail" class="col-sm-2 col-form-label">Email</label>
                <div class="input-group">
                    <div class="input-group-prepend">
                        <span class="input-group-text" id="validationTooltipUsernamePrepend">@</span>
                    </div>
                    <input type="email" class="form-control" id="validationTooltipEmail" placeholder="Email" 
                    aria-describedby="validationTooltipUsernamePrepend" required>
                    <div class="invalid-tooltip">
                        Please choose a valid email.
                    </div>
                </div>
            </div>

            <div class="form-group row">
              <label for="validationTooltip03" class="col-sm-2 col-form-label">Query</label>
              <div class="input-group">
                    <input type="text" class="form-control" id="validationTooltipQuery" placeholder="Query" required>
                    <div class="invalid-tooltip">
                        Please write your Query.
                    </div>
                </div>
            </div>
            <div class="btn-group offset-md-5">
                <button class="btn btn-primary" type="submit">Submit</button>
                <button class="btn btn-default" type="button" id="homebtn" ng-click="home()">Home</button>  
            </div>
          </form>
    </div>
</div

contact.component.js

angular.module('myApp')
  .component('contactComponent', {
  restrict: 'E',
  scope: {},    
  templateUrl:'contact/contact.html',
  controller: contactController,
  controllerAs: 'vm',
  // bindings:{
  //   information:'='
  // }

});

function contactController() {
 };

app.js

var app = angular.module('myApp', ['ui.router']);
app.controller('mainCtrl',function(){

});

app.config(['$stateProvider', '$urlRouterProvider',function($stateProvider,$urlRouterProvider){
    $urlRouterProvider.otherwise('/');

    $stateProvider
      .state('home', {
        url: '/home',
        templateUrl: 'home.html'
      })
      .state('dashboard', {
        url: '/dashboard',
        component: 'dashboardComponent'
      })
      .state('contact', {
        url: '/contact',
        component: 'contactComponent'
      })
      .state('about', {
        url: '/about',
        component: 'aboutComponent'
      })

}]);

使用

  <button class="btn btn-default" type="button" id="homebtn" ng-click="navigate ('home')">Home</button>  

在控制器中,

$scope.navigate = function(routeName){
  $state.go(routeName)
};

我注意到你写了

ngController="contactController"

应该是

<div ng-controller="contactController">

之后更改您的 contactController 如下。放在模块初始化之后。

app.controller('contactController', ['$scope', '$state', function ($scope, $state) {
        $scope.home = function () {
            $state.go('home');
        };
    }]);

我终于让它工作了。

    function contactController($scope, $state) {
  $scope.navigate = function(home){
    $state.go(home)
  };

};

我在控制器函数中缺少 $scope 作为参数。