Marius Mutu
Marius Mutu

Reputation: 65

ng-click does not update model

I have an AngularJS test page with a "Refresh" link which calls a function, which, in return, updates the model "customer.name"

However, the "Refresh" click still doesn't change the {{customer.name}} in the view and I don't understand why.

Here is the content of my application.

index.html

<!doctype html>
  <head>
    <title>Test</title>
</head>
  <body ng-app="roaMobileNgApp">


    <script src="scripts/angular.js"></script>

    <script src="scripts/angular-ui.js"></script>
    <link rel="stylesheet" href="scripts/angular-ui.css">

    <div class="container" ng-view=""></div>

    <script src="scripts/app.js"></script>
    <script src="scripts/controllers/main.js"></script>


</body>
</html>

app.js

'use strict';

angular.module('roaMobileNgApp', ['ui'])
  .config(function ($routeProvider) {
    $routeProvider
      .when('/', {
        templateUrl: 'views/main.html',
        controller: 'MainCtrl'
      })
      .otherwise({
        redirectTo: '/'
      });
  });

main.js

'use strict';

angular.module('roaMobileNgApp')
  .controller('MainCtrl', function ($scope) {


    $scope.customer = {name: '',
                      id: 0};



    $scope.getDetails = function() {
        $scope.customer.name = 'Test';
    };

  });

main.html

    <a href="#" ng-click="getDetails()">Refresh</a>
    <p><input type="text" ng-model="customer.name"> {{ customer.name }} </p>

</div>

Upvotes: 1

Views: 6069

Answers (1)

AlwaysALearner
AlwaysALearner

Reputation: 43947

This is probably because you are changing the address by clicking:

<a href="#" ng-click="getDetails()">Refresh</a>

This forces angular to re-render the html as well as the controller.

Change it as below:

<a href="javascript:void(0)" ng-click="getDetails()">Refresh</a>

Upvotes: 4

Related Questions