Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm still new with angularJS. I've been trying to make a custom button and attach it to my form instead of using regular button. I've tried couple of approaches and so far none of them worked well. now when I press enter inside the input field I get the "results" view perfectly loaded to the main page. but when I click the search button "a" link tag the view loads then disappears instantly. as well as the location of the browser changes to "results" then goes back to "/#/" only. I have no idea why and what's causing this.

here's my html:

<div id="search-container" ng-controller="SearchController">
  <form ng-submit="submitQuery()">
    <div>
      <input id="keywords" name="keywords" ng-model="query.keywords"  placeholder="please enter query" value="" required/><br>
      <a href="#" id="search-btn" ng-click="submitForm()"><img src="/Images/search-icon.png" alt="Search" title="Search" /></a>
    </div>
  </form>
</div>

here is my model and ngjs controllers:

var bfapp = angular.module("blogfinder", []).config(function ($routeProvider) {
  $routeProvider.when('/results', {
    templateUrl: 'PartialViews/results.html',
    controller: 'ResultsController'
  });

  $routeProvider.otherwise({ redirectTo: '/' });
});

bfapp.controller('ResultsController', function ($scope) {
});

bfapp.controller('SearchController', function ($scope, $location) {
  $scope.query = { keywords: "" };

  //on form submit
  $scope.submitQuery = function () {
    if ($scope.query.keywords !== null) {
      $location.path('/results');
    }
  };

  //on button click
  $scope.submitForm = $scope.submitQuery;
});
share|improve this question

1 Answer 1

up vote 1 down vote accepted

well I feel so stupid. I've just found the solution after banging my head for couple of hours. Although this has never been mentioned on any site. All I needed is to remove "#" from <a href="#" id="search-btn" ng-click="submitForm()">. Now it works like charm.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.