angular-xeditable icon indicating copy to clipboard operation
angular-xeditable copied to clipboard

Unable to update value without page refresh

Open pravinkumart opened this issue 10 years ago • 10 comments

I try to set a value based on value in another field . The scope gets updated, but xeditable cant update in form.

I tried the same using plain angularjs without xeditable just using ng-model.It works.But with xeditable its not working.

Here is the sample js fiddle link : http://jsfiddle.net/pravinkumart/3Mc93/

Please some one help me to fix it.

pravinkumart avatar Jun 03 '14 09:06 pravinkumart

Use $watch

    $scope.$watch('a', function (val) {
        $scope.b = val; 
    });

JSFiddle

tthew avatar Jun 04 '14 14:06 tthew

Thank you for your solution. But i would like to do it in form like this.. http://jsfiddle.net/pravinkumart/BfL9D/2/

When i start typing or at the end of type(focus out of the box), the value in the name text box should reflect in user name text box before clicking on save button.

pravinkumart avatar Jun 05 '14 07:06 pravinkumart

I have a similar problem. I am updating the model within a directive. The model is correctly updated in the scope of the controller, but the view does not change. I also tried to manually trigger $scope.$apply(), but nothing happens.

pasine avatar Aug 22 '14 15:08 pasine

As I see it, it's just not possible because xeditable deliberately detaches field value from the model to perform its own data checks (onbeforesave, etc.) before updating the model back. So it's not an issue but a feature)

uqee avatar Sep 03 '14 17:09 uqee

I understand this, but is this necessary? Wouldn't it be easier to deal directly with the model? Wouldn't this add out of the box features like native validation and two-way binding?

pasine avatar Sep 09 '14 08:09 pasine

Hi, I solved this problem by setting the value of the input via angular.element.

angular.element($('input[name="myInputName'"]')).val("myInputValue");

ahneo avatar Sep 10 '14 04:09 ahneo

The problem on setting the value through the use of jQuery/jQlite is that the form won't know the value has changed and the form states won't be updated like if the form is $valid or $invalid.

I have found a solution but it is a little hard coded, from the forms controller access the property $editables and select the arrat element corresponding to the input you want to update and update the value in scope.$data. Here is the full code:

// Inside your controller
$scope.formName.$editables[0].scope.$data = newValue;

Another way I found but it required editing the source code is to add the following line inside the watch on line 435:

// Line 435 inside /angular-xeditable/dist/js/xeditable.js
$scope.$parent.$watch($attrs[self.directiveName], function(newVal, oldVal) {
  self.setLocalValue();
  self.handleEmpty();
});

So that everytime the main model value assigned to the field changes it updates the internal model.

joseaquino avatar Nov 05 '14 10:11 joseaquino

Hello, i have the same problem. Your first solution works but not the second for me. anyway, i use also some validation with pattern and when we update with :1234: $scope.formName.$editables[0].scope.$data = newValue; it does'nt works because of the pattern validation : get undefined value in the input.

see : http://jsfiddle.net/kirakishin/4o7jkj4a/

anyone has an idea please ?

i think to not use ng-pattern but the custom validation of xeditable with onebeforesave.

kirakishin avatar Dec 05 '14 14:12 kirakishin

@joseaquino I have used your 1st solution.It's working fine.Thanks for the Great work :)

Sampath-Lokuge avatar Jul 10 '15 06:07 Sampath-Lokuge

Solution by @joseaquino works but it is definitely a workaround. Form should watch changes of the scope and updated itself.

eugef avatar Jul 15 '15 14:07 eugef