1

I'm baffled here I've got a controller which looks like this..

angular.module('publicRegApp')
  .controller('CompanyNameController', function ($scope) {
    $scope.title = 'Title One';

    $scope.onSearch = function(){
        console.log("clicked");
        $scope.title = "Title Two";
    }

  });

in my template I have:

<button type="submit" class="btn btn-default" ng-click="onSearch()">Search</button>

When I click the button the console log gets fired but the $scope.title doesn't change. I'm so confused please help :-(

9
  • 1
    where is the title injected in your view? how do you conclude it does not change? Commented Mar 4, 2014 at 12:31
  • 1
    Check your title by wrapping it like <div ng-controller="CompanyNameController">{{title}}</div> Commented Mar 4, 2014 at 12:37
  • The {{title}} is contained within a view, I am assigning View and Controller via the modules config $routeProvider - {{title}} gets set fine when I load up the page but just won't change when I click? Commented Mar 4, 2014 at 12:41
  • 2
    change type="submit" to type="button" Commented Mar 4, 2014 at 12:42
  • 1
    And thanks Vicky for your constructive comment criticism Commented Mar 4, 2014 at 12:48

3 Answers 3

3

It works if you take care of these things:

  1. Make sure you have ng-app="publicRegApp" somewhere
  2. Correct you Javascript from angular.module("publicRegApp") to angular.module("publicRegApp", []) which indicate that you don't depended on other angular module.
  3. Make sure {{title}} is in the controller scope

Here is working example: http://jsbin.com/xotaf/5/edit

Sign up to request clarification or add additional context in comments.

Comments

1

My guess is that your {{title}} in template is outside of scope for controller. I would strongly recommend to check it ;)

$scope.$apply is very bad suggestion, it's not doing what you think it's doing

------------EDIT---------

check this plunker pls

http://plnkr.co/edit/0LyUaGWK4J5c9EY74ndw?p=preview

4 Comments

My guess is that your {{title}} in template is outside of scope for controller. I would strongly recommend to check it Then how to first time it working?
Hey thanks for reply, I'm not sure how {{title}} could be out of scope as it gets set to 'Title One'. The problem is it won't update to 'Title Two'?
@user1803975 Have you tried this <div ng-controller="CompanyNameController">{{title}}</div>
I've edited post with plunker, it seems to work, please check it out
1

Not totally sure why but having

<button type="submit">

was the issue. Changing to

<button type="button">

fixed it.

I then realised I should probably be using ng-submit on the buttons's parent element which also fixed the problem (as long as I leave the button as submit of course)

1 Comment

eaither you choose <input type="button"> or <button></button>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.