error passing value from a ng-model to another ng-model using ng-change

163 Views Asked by At

For the first input of 'a.html', user can select from the drop down list or enter a value.

For the second input, the default value is set to be the same as first input. User can later change the value in the second input. The value from the second input will then pass to function setPath for b.js.

After I have input a value 'android' in the first input, the second input will then default to 'android' as well. I am getting console error after I have removed all values in the second input and trying to input a new value. It says " cannot create property name on string 'android'.

a.html

 // first input
<input type="text" ng-model="result.c" data-auto-select="true"
       ng-change="result.path = result.c; checkIfMatch()"
       ng-blur="checkIfMatch()" bs-options="c as c.name for c in cs" />

 // second input
<input type="text" ng-disabled="!result.c.id" ng-model="result.path.name"/>

a.js

  $scope.checkIfMatch = function(){
      if(!$scope.result.c || $scope.result.c && $scope.result.c.id){
         return;
      } else{  
         var c = $scope.result.c;
          for(var i = 0; i < $scope.cs.length; i++){
            if(c == $scope.cs[i].name){
              $scope.result.c = $scope.cs[i];
              return;
            }
          }
      }
  };

b.js

function setPath() {
   $modal.open({
       templateUrl: '/xxx/a.html',
       scope: scope,
       controller: 'aController',
       windowClass: 'aModal'
      }).result.then(function(result){
     // scope.result.path.name to be the same value as second input in a.html
         var anchorTag = '<a href="' + result.c.id +'">Launch '+  result.path.name + '</a>';
 }

if i change the code for ng-change of first input at a.html, the value doesn't show up for the second input after moving cursor out of the first input. Still new to twitter bootstrap bs-options.

<input type="text" ng-model="result.c" data-auto-select="true"
       ng-change="result.path = result.c.name; checkIfMatch()"
       ng-blur="checkIfMatch()" bs-options="c as c.name for c in cs" />

// second input

<input type="text" ng-disabled="!result.c.id" ng-model="result.path"/>
0

There are 0 best solutions below