0

I have a very weird problem with AngularJS 1.4 (TypeScript). The problem is in the controller I have a variable and this value can be viewed in an input text box. However, when I edit the value in this text box and click on a button, the value of this variable doesn't change(!).

HTML view

<div class="form-group">   
     <label>Title</label>
     <input class="form-control" ng-model="serviceTitle">
</div>


<div class="form-group">
     <button class="btn btn-primary" ng-click="updateServiceIdentification()">Update Service Identifcation</button>
</div>

Controller:

 $scope.serviceTitle = "Test";

 $scope.updateServiceIdentification = ()=> {
    // after changing value in view, the value here is still "Test"????
    alert($scope.serviceTitle);
 }

If I add a test label in HTML view

<h1>{{serviceTitle}}</h1>

when I change the value of the input text box, the new value can be printed in this label.

Aleksey Solovey
  • 4,153
  • 3
  • 15
  • 34
Bằng Rikimaru
  • 1,512
  • 2
  • 24
  • 50

1 Answers1

0

The problem comes from this and caused me few hours. If I add text box under this element, it cannot work. Put it outside, the alert() returns the changed value from the text box.

    <input class="form-control" ng-model="serviceTitle">
    <input type="button" class="btn btn-primary" ng-click="updateServiceIdentification()" value="Update Service Identifcation"/>
    <accordion>
            <accordion-group>
              <accordion-heading>
                        some text here, on the left side of the header.

                        <div class="pull-right">
                          <span>1st info</span>
                          <span>2nd info</span>
                          <span>maybe 3rd?</span>
                        </div>                
              </accordion-heading>
             <!------------ Doesn't work if it is inside ------------->
            </accordion-group>    
          </accordion>

The real problem should be "it's because a string is a primitive. When Angular assign the value it change the pointer to the value, so the controller looks at the old value because it have the old pointer to the value." Damax - Ng-model does not update controller value

Update Controller like this:

$scope.output = {serviceTitle: "1234567"};                
$scope.serviceTitle = "asdadasdsad";

$scope.updateServiceIdentification = (...args: any[])=> {
     alert($scope.output.serviceTitle);
}

and in view it can work under accordion elements.

   <uib-accordion close-others="false">
       <uib-accordion-group is-open="isAvailableLayersOpen">
           <input class="form-control" ng-model="output.serviceTitle">
           <input type="button" class="btn btn-primary" ng-click="updateServiceIdentification()" value="Update Service Identifcation"/>
       </uib-accordion-group>
  </uib-accordion>
Bằng Rikimaru
  • 1,512
  • 2
  • 24
  • 50