2016-09-27 5 views
0

ich brauche, basierend auf einer in Seitenvariable, Wert eines Bindeelementes zu ändern.AngularJS wie dynamisch Wert von Bindungselement setzen

Hier ist es die in Seite var:

<script type="text/javascript"> 
var myVar = 'value1'; 
</script> 

Dies ist die select mit dem Bindeelement:


<select value=""> 
<option ng-repeat="value in valueList"> 
    {{myNewValue()}} 
</option> 
</select> 

Hier ist die Steuerung:


$scope.inPageVar = $window.myVar; 

$http.get(jsonUrl).then(function(result) { 
    $scope.valueList = result.data.valueList; 
    $scope.myNewValue = function() { 
     return $scope.valueList[0].additional + $scope.inPageVar; 
    }; 
}); 

Hier ist die JSON-Struktur:


"valueList": [ 
      { 
      "additionnal": { 
       "value1": "this is value 1", 
       "value2": "this is value 2" 
      } 
      }, 
      { 
      "additionnal": { 
       "value1": "this is value 3", 
       "value2": "this is value 4" 
      } 
      } 
     ] 

Ich mag würde wissen, wie die in-Seite var Wert mit dem Element der get-Funktion verketten .. Thank Sie!

+0

Sie können dies nicht erreichen, bis Sie die meineVar als global machen oder fügen Sie $ rootScope – Thaadikkaaran

+0

Wie kann ich das tun? – paolopolix

+0

Möchten Sie eine Javascript-Variable im Winkel-Controller zuweisen? –

Antwort

0

(function() { 
 

 
    angular.module("CombineModule", []).controller('MyController', function() { 
 
    this.values = [{ 
 
     "additional": { 
 
     "value1": "this is value 1", 
 
     "value2": "this is value 2" 
 
     } 
 
    }, { 
 
     "additional": { 
 
     "value1": "this is value 3", 
 
     "value2": "this is value 4" 
 
     } 
 
    }]; 
 

 
    this.myNewValue = function(index) { 
 
     return this.values[index].additional[myVar]; 
 
    }; 
 
    }); 
 

 
})();
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> 
 
<!DOCTYPE html> 
 
<html lang="en" ng-app="CombineModule"> 
 

 
<head> 
 
    <title></title> 
 
    <meta charset="UTF-8"> 
 
    <meta name="viewport" content="width=device-width, initial-scale=1"> 
 
    <script> 
 
    var myVar = 'value1'; 
 
    </script> 
 
</head> 
 

 
<body ng-controller="MyController as v"> 
 
    <select value=""> 
 
    <option ng-repeat="value in v.values"> 
 
     {{v.myNewValue($index)}} 
 
    </option> 
 
    </select> 
 
    <script src="app.js"></script> 
 
</body> 
 

 
</html>

Wenn Sie meineVar global verfügbar machen möchten,

Schreib window.myVar = 'some value' statt var myVar = 'someval'.

Oder wenn Sie diese in $rootScope.myVar = 'some value' zur Verfügung stellen möchten

Dann in Ihrer Logik,

return $scope.valueList[0].additional[window.myVar];

+0

die myVar stammt aus dem Backend und es wird innerhalb der HTML in einer

Verwandte Themen