Need help on simple data binding that mysteriously gone away randomly(no controller, only pure html and AngularJS) -
i have simple html try angularjs databinding. when enter text in input, label below binds correctly, , displays data.country. however, when use backspace delete text until gone, , try reenter text again, nothing show on bound span. problem seems random, since if keep repeating (enter text, delete, enter text) shows up, doesn't
here fiddle http://jsfiddle.net/supatwo/emfnp/
at first glance, thought random, in fact pattern. 1. enter text,e.g. "abcde". binds correctly span below input box. 2. backspace until text gone. 3. re enter text, e.g. 12345. bug? nothings show in span below input box. 4. backspace until text gone. 5. enter text, shows correctly, , keep repeating (steps 1-5).
i use batarang check here has $rootscope, , batarang shows no model in $rootscope. if put model in controller, there no issue.
<!doctype html> <html lang="en" ng-app> <head> <meta charset="utf-8"> <title>my html file</title> <link rel="stylesheet" href="css/app.css"> <link rel="stylesheet" href="css/bootstrap.css"> <script src="lib/angular/angular.js"></script> </head> <body> country name: <input type="text" ng-model="data.country"></input> <br/><span class="label"> {{data.country}}</span> <br/> </body> </html>
take @ code (http://jsfiddle.net/denisonluz/9mfpp/245/). working.
<!doctype html> <html lang="en" ng-app="myapp"> <head> <meta charset="utf-8"> <title>document</title> <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.1.5/angular.min.js"></script> <script> var myapp = angular.module('myapp', []) .controller('mainctrl', function($scope) { $scope.data = {country: 'brazil'}; $scope.set = function(new_country) { this.data.country = new_country; } }); </script> </head> <body> <div ng-controller="mainctrl"> <input name="data.country" ng-model="data.country"><br /> <span>{{data.country}}</span><br /> <button ng-click="set('italy')">set italy</button> </div> </body> </html>
about issue facing, thing think of should define model in controller, not in view. (https://stackoverflow.com/a/10612424/1310945)
Comments
Post a Comment