下面的代码在具有不同控制器的一页内工作,但是当我将值从一页传递到另一页时,scope.watch 不起作用。你怎么能那样做?下面是我的代码。
.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 INTO 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 />