angular - index.html仅识别默认路由中定义的函数

时间:2015-02-18 19:58:47

标签: javascript angularjs

我有角度问题。 在我的index.html中我有导航栏,其中包含ng-click =" go('')"对于每个标签。

'去'是我的每个控制器中定义的函数。

var sheepApp= angular.module('sheepApp', [
    'ui.bootstrap',
    'ngRoute',
    'firebase'
]);

sheepApp.config(['$routeProvider',
  function($routeProvider) {
    $routeProvider.
      when('/home', {
        templateUrl: 'pages/home.html',
        controller: 'homeCtrl'
      }).
      when('/vision', {
        templateUrl: 'pages/vision.html',
        controller: 'visionCtrl'
      }).
      when('/about', {
        templateUrl: 'pages/about.html',
        controller: 'aboutCtrl'
      }).
      when('/gallery', {
        templateUrl: 'pages/gallery.html',
        controller: 'galleryCtrl'
      }).
      when('/videos', {
        templateUrl: 'pages/videos.html',
        controller: 'videosCtrl'
      }).
      otherwise({
        redirectTo: '/home'
      });
  }]);

问题在于' go'功能(以及所有其他功能)仅在我现在定义为'否则' (就我而言 - ' home')

为什么会发生?

感谢

修改

我在ctrls.js中定义的所有conntoller

其中一个ctrls的例子:

sheepApp.controller('joinUsCtrl', ['$scope','$rootScope','$location',
    function ($scope, $rootScope,$location) {
        $rootScope.go = function ( path ) {
            $location.path( path );
        };

        $rootScope.isActive = function(tabName){
                return $location.path() == tabName;
        };

        $scope.he = he;
}]);

1 个答案:

答案 0 :(得分:0)

这是$ rootScope中的工作方法示例。 检查你的chrome控制台如果抛出错误然后没有添加控制器这行给我一个错误 $ scope.he = he; 并且go函数没有改变。 (基于你的例子改变了小事)。

var sheepApp= angular.module('sheepApp', [
    'ngRoute'
]);

sheepApp.config(['$routeProvider',
  function($routeProvider) {
    $routeProvider.
      when('/home', {
        template: '<div>{{controller.name}}</div>',
        controller: 'homeCtrl'
      }).
      when('/vision', {
        template: '<div>{{controller.name}}</div>',
        controller: 'visionCtrl'
      }).otherwise({
        redirectTo: '/home'
      });
  }]);


sheepApp.controller('homeCtrl', ['$scope','$rootScope','$location',
    function ($scope, $rootScope,$location) {
        $rootScope.go = function ( path ) {
            alert("works in home");
        };
      
      $rootScope.asd = {asd:"asd"};

        $rootScope.isActive = function(tabName){
                return $location.path() == tabName;
        };
      $scope.controller={name:'home'}
}]).controller('visionCtrl', ['$scope','$rootScope','$location',
    function ($scope, $rootScope,$location) {
        $rootScope.go = function ( path ) {
            alert("works in vision");
        };
      
      $rootScope.asd = {asd:"asd"};

        $rootScope.isActive = function(tabName){
                return $location.path() == tabName;
        };
      $scope.controller={name:'vision'}
}]);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular-route.js"></script>

<div ng-app="sheepApp">
  <button ng-click="go()">RootScope function</button>
  <a href="#/vision" > vision</a>
  <div ng-view></div>
  </div>