简体   繁体   中英

Pass Javascript variables to Angularjs controller

My question is in reference with this one. Pass JavaScript variable into AngualrJs ng-init

This was asked for passing a single javascript variable being passed to the angular controller. can someone let me know how can i pass several variables from a script to controller instead of a single variable ?

Assuming that your variables are not in any special scope...

window.var1 = true;
var var2 = "foo";
var3 = {foo: "bar"};

var app = angular.module('myapp', []);

app.controller('MainCtrl', ['$scope', '$window', function($scope, $window) {
  $scope.var1 = $window.var1;
  $scope.var2 = $window.var2;
  $scope.var3 = $window.var3;
}]);

Basically copy-pasted from linked question. Pass JavaScript variable into AngualrJs ng-init

Exactly the same way. Inject the $window service on your controller and all the variables outside angular mode will be available to the controller through:

$scope.ControllerVariable = $window.ScriptVariable

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM