2014-02-20 80 views
0

我是angularJS的新手,尝试创建一个简单的应用程序。当我点击一个按钮,比如index.html页面,我想转到.search.html页面时,我遇到了问题。angularJS - 简单的按钮导航页面

这里是我的index.html的按钮:

<body ng-controller="mainController"> 
    <button ng-click="GoToNext2('/search')">Search</button> 
</body> 

这里是我的角度代码:

// create the module and name it scotchApp 
var scotchApp = angular.module('scotchApp', ['ngRoute']); 

// configure our routes 
scotchApp.config(function($routeProvider) { 
$routeProvider 

    // route for the home page 
    .when('/', { 
     templateUrl : 'home.html', 
     controller : 'mainController' 
    }) 

    .when('/search', { 
     templateUrl : 'Search.html', 
     controller : 'searchController' 
    }); 
}); 

// create the controller and inject Angular's $scope 
scotchApp.controller('searchController', function($scope) { 

$location.path(hash); 

}); 

我要调用一个函数(执行当我点击过搜索)

谢谢

+0

你到目前为止尝试了什么?首先要在你的控制器中实现你的'goToNext()'方法。 – muenchdo

+0

可能的重复[是否有一个简单的方法来使用按钮导航页面作为链接在angularjs中执行](http://stackoverflow.com/questions/15847726/is-there-a-simple-way-to-use-按钮浏览的页面,作为一种链路确实功能于angularjs) –

回答

0

正如评论中指出的那样,看起来你缺少一个goToNext()方法,你w乌尔德实现它想:

scotchApp.controller('mainController',[ 
    '$scope', '$location', 
    function($scope, $location) { 

     $scope.GoToNext2 = function(hash) { 
      $location.path(hash); 
     }; 
    } 
]); 

旁边的是,它是好的,有路线后备..默认: $routeProvider.otherwise({redirectTo : '/'})其中如果用户导航到一个不存在的页面被触发。

希望这会有帮助