将Javascript变量传递给Angularjs控制器

将Javascript变量传递给Angularjs控制器

本文介绍了将Javascript变量传递给Angularjs控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题与此有关.将JavaScript变量传递到Angualrs ng-init

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

有人要求将单个javascript变量传递给角度控制器.有人可以让我知道如何将脚本中的多个变量而不是单个变量传递给控制器​​吗?

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;
}]);

基本上从链接的问题复制粘贴. 将JavaScript变量传递到Angualrs ng-init

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

这篇关于将Javascript变量传递给Angularjs控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 02:32