There is issue $scope value doesn't bind to ng-model at first time load

915 views Asked by At

I want to display some default template in text area at very first loading.I tried to get that output but my code doesn't work.I put all breakpoints that value change.that also doesn't work. When second time data will display on text area.

<textarea meditor style="height: 100%;margin-top: 10px;border: 1px solid lightgray;" ng-model="activity.GoCode" name="gocode" required>{{activity.GoCode}}</textarea>

function getGoCode(data) {
    if (data.GoCode == undefined) {
           $scope.activity.GoCode = "test";
    } else {
          $scope.activity.GoCode = data.GoCode;
          };
    }
2

There are 2 answers

1
Lakmi On BEST ANSWER

I Found solution for this I set a value initially for this variable (activity.GoCode) before that I create '$scope.activity' object and then assign value for when data is loaded.

   $scope.ReadSampleGoFile = function () {

        $http.get('settings/sampleGO.txt').then(function (data) {
            //use the file data here
            // console.log(data.data);
             $scope.activity=[];
             $scope.activity.GoCode=data.data;
        });
    } 
1
Tonio On

When does your function getGoCode() is called, upon page load ?

I see one issue with your code even if it is not probably the problem here, having:

ng-model="activity.GoCode"

and

{{activity.GoCode}}

is redundant as explain here and will cause an exception in Internet explorer (Error: Invalid Argument) as explain here

Also what is the meditor directive doing, it is possible that this directive is the source of your problem as I don't see why your code would not work