, , , - . - $scope.$watch $scope.user, MyFactory.getOptions.
angular.module('users').controller('MyController', ['$scope', 'Authentication', 'MyFactory',
function($scope, Authentication, MyFactory) {
$scope.user = Authentication.user;
$scope.options = MyFactory.getOptions($scope.user.firstName, $scope.user.lastName);
$scope.$watch("user", function(newVal,oldVal,scope) {
scope.options = MyFactory.getOptions(newVal.firstName, newVal.lastName);
});
...
}
...
}
- . , .
- :
angular.module('users').controller('MyController', ['$scope', 'Authentication', 'MyFactory',
function($scope, Authentication, MyFactory) {
$scope.user = Authentication.user;
$scope.options = MyFactory.getOptions($scope, "user");
...
}
...
}
angular.module('users').factory('MyFactory',
function() {
var _this = this;
_this._data = {
getOptions: function(scope, property){
var updateableArray = [];
function updateArray(user) {
updateableArray.clear();
updateableArray.push(firstName + ' ' + lastName);
updateableArray.push(lastName + ' ' + firstName);
....
}
scope.$watch(property, function(newVal,oldVal,watchScope) {
updateArray(newVal);
});
updateArray(scope[property]);
return updateableArray;
}
};
return _this._data;
}
);
, , , , , .