0

I have created a factory in my project as follows,

app.factory('userFactory', userFactory);

function userFactory() {
var userInitialValueSettings = {
    UserId: '',
    FullName: '',
    Location: '',
    Phone: '',
    Ext: '',
    Fax: '',
    Email: '',
    IsExternalUser: false,
    Password: '',
    UserMustChangePassword: false,
    PasswordNeverExpires: false
}

return {
    userInitailValues: userInitialValueSettings
}
}

I used the userInitialValues object of the factory to initialize one of my scope object as given below.

$scope.user = userFactory.userInitailValues;

I used the properties of $scope.user as the models of some input textboxes. I have a clear button in my form. I need to clear the textboxes while clicking the clear button. So I hooked the following function in the clear button click.

$scope.cancelAddUserDialoge = function () {
    $scope.user = userFactory.userInitailValues;
}

But the text fields are not getting cleared. What is the issue? Can anybody help me? If I use the following method in the clear button click and clear each model property individually then, the textboxes are getting cleared.

function ResetUserModel() {
    $scope.user.UserId= '',
    $scope.user.FullName = '',
    $scope.user.Location = '',
    $scope.user.Phone = '',
    $scope.user.Ext = '',
    $scope.user.Fax = ''
    $scope.user.Email = '',
    $scope.user.IsExternalUser = false,
    $scope.user.Password = '',
    $scope.user.UserMustChangePassword =false,
    $scope.user.PasswordNeverExpires =false
}

Why I cannot use the factory object to clear the models? Please help me.

2 Answers 2

1

You are assiging object without copying it

$scope.user = userFactory.userInitailValues;

Object will keep reference during assigning .

If one side is changed another will automatically be changed.

So when you assigned without using copy,you change in form will also make change in userInitailValues. ultimately you are re-assigning $scope.user in the $scope.user.

Try like this

$scope.user = angular.copy(userFactory.userInitailValues);
Sign up to request clarification or add additional context in comments.

3 Comments

Probably better to make userFactory.userInitialValues a function that returns a copy then the caller(s) don't have to always remember to copy before use.
Thank you for your reply. I already tried it. But it didn't worked. I found that, it is 2 way updating. So I used angular.copy as you suggested. It is working in initialization while loading. But one clear button click the text fields are not getting clear.
@NOBLEM.O. can you make a fiddle and provide link to me ?
1

You initialize the values only once. Then $scope.user links to the values and you change these values directly. I would suggest you to create initial data next way:

app.factory('userFactory', userFactory);

function userFactory() {

    return { 
      init : function() {
        return { 
          UserId: '',
          FullName: '',
          Location: '',
          Phone: '',
          Ext: '',
          Fax: '',
          Email: '',
          IsExternalUser: false,
          Password: '',
          UserMustChangePassword: false,
          PasswordNeverExpires: false 
        };
      }
   };
}

Then in you controller init data

$scope.user = userFactory.init();

and reset

$scope.cancelAddUserDialoge = function () {
    $scope.user = userFactory.init();
}

Here the fixed fiddle http://jsfiddle.net/takf64v6/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.