Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJS data binding not working

Tags:

angularjs

bind

I wrote some simplified code on http://jsfiddle.net/gsche/1/

When I click the "Refresh" link, the "customer.name" model doesn't update the view.

I wrote the code on my local computer and debugged it with Batarang in Chrome.

The console doesn't show any errors. The Model page in Batarang shows the customer name changing on the right, associated with an old scope id, but the id's of the $scopes also change.

Can anyone point me in the right direction?

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


function MainCtrl($scope) {


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

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

    };

}

Update 1 08.08.2013 Thank you all (@EpokK, @Stewie, @Hippocrates). I undestand now the problem with jsfiddle, and the example works as it should.

However, in my test application, the {{customer.name}} binding works, but the "Refresh" click still doesn't change the {{customer.name}} in the view.

Here is the content of my application. I think it is the same as the jsfiddle example:

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

<div ng-controller="MainCtrl">

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

</div>
like image 315
Marius Mutu Avatar asked Aug 07 '13 14:08

Marius Mutu


1 Answers

Check my solution, I have edit your JSFiddle: http://jsfiddle.net/gsche/10/

function MainCtrl($scope) {

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

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

}
like image 155
EpokK Avatar answered Sep 27 '22 22:09

EpokK