angularjs - $scope.watch does not work between two different angular pages -
the below code working within 1 page different controlloers, scope.watch not work when pass value 1 page another. how can that? below code.
.factory('data', function () { var data = { lastname: '', } return { getlastname: function () { return data.lastname; }, setlastname: function (lastname) { data.lastname = lastname; } } } //first controller $scope.lastname = ''; $scope.$watch('lastname', function (newvalue2, oldvalue2) { if (newvalue2 !== oldvalue2) data.setlastname(newvalue2); }); //get first controller second $scope.$watch(function () { return data.getlastname(); }, function (newvalue2, oldvalue2) { if (newvalue2 !== oldvalue2) $scope.lastname = newvalue2; }); //form //firstcontroller < input type = "text" name="lastname" placeholder = "suhr" ng-model="lastname" ng-minlength="3" required />
porting code , works!
angular.module("test", []) .controller('test1', test1) .controller('test2', test2) .factory('data', data); function test1($scope, data) { $scope.lastname = ''; $scope.$watch('lastname', function(newvalue2, oldvalue2) { if (newvalue2 !== oldvalue2) data.setlastname(newvalue2); }); } function test2($scope, data) { $scope.$watch(function() { return data.getlastname(); }, function(newvalue2, oldvalue2) { if (newvalue2 !== oldvalue2) $scope.lastname = newvalue2; }); } function data() { var data = { lastname: '', } return { getlastname: function() { return data.lastname; }, setlastname: function(lastname) { data.lastname = lastname; } } }
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.8/angular.min.js"></script> <div ng-app="test"> <div ng-controller="test1"> <input type="text" name="lastname" ng-model="lastname" placeholder="suhr" ng-minlength="3" required> </div> <div ng-controller="test2"> {{lastname}} </div> </div>
Comments
Post a Comment