Javascript 如何访问在其他js文件中计算的变量?
仅举一个简单的例子说明: 在本例中,如何使用angularApp.js中计算的机器长度? 现在,它输出一个空对象,而不是angularApp.js中计算的值Javascript 如何访问在其他js文件中计算的变量?,javascript,html,angularjs,variables,scope,Javascript,Html,Angularjs,Variables,Scope,仅举一个简单的例子说明: 在本例中,如何使用angularApp.js中计算的机器长度? 现在,它输出一个空对象,而不是angularApp.js中计算的值 <html> <head> <script src="...">...</script> <script> let machineLength={}; </script> <script src="angularAp
<html>
<head>
<script src="...">...</script>
<script>
let machineLength={};
</script>
<script src="angularApp.js"></script>
</head>
<body>
...
<script>
console.log(machineLength);
// here is a lot of code use machineLength to render scene of models with three.js
//the machineLength is the render region width, height. length
</script>
</body>
</html>
任何帮助都将不胜感激 您需要使用
你需要使用
问题是
angularApp.js
使用异步ajax调用,并且主页中的脚本在响应到达之前打印machinelelength
一个解决方案是让angularApp脚本在响应到达时通知主页中的脚本。在angularApp.js中:
let sliceApp = angular.module('sliceConfig', []);
sliceApp.
.controller('sliceConfigController', ['$scope', '$http', function($scope, $http)
{
$http.get("http://xxxxx/getSliceConfig")
.success(function(response)
{
$scope.config = [];
$scope.config.machine_width = machineLength.x = parseFloat(response.config.machine_width);
...
}
}]);
...
.success(function(response) {
$scope.config = [];
$scope.config.machine_width = machineLength.x = parseFloat(response.config.machine_width);
angularFinishedJob(); // call the script in the main page when job is done
...
主页:
<script>
console.log(machineLength); // empty object, because angular job didn't finish
function angularFinishedJob(){ // called from angularApp.js
console.log(machineLength); // calculated object
// add code which uses machineLength
}
</script>
控制台日志(机器长度);//空对象,因为角度作业未完成
从angularApp.js调用函数angularFinishedJob(){//
console.log(machinelelength);//计算对象
//添加使用machineLength的代码
}
注意:如果您有权修改
angularApp.js
,我认为这将是最容易实现的方法。如果没有,则存在其他解决方案问题在于angularApp.js
使用异步ajax调用,并且主页中的脚本在响应到达之前打印machinelelength
一个解决方案是让angularApp脚本在响应到达时通知主页中的脚本。在angularApp.js中:
let sliceApp = angular.module('sliceConfig', []);
sliceApp.
.controller('sliceConfigController', ['$scope', '$http', function($scope, $http)
{
$http.get("http://xxxxx/getSliceConfig")
.success(function(response)
{
$scope.config = [];
$scope.config.machine_width = machineLength.x = parseFloat(response.config.machine_width);
...
}
}]);
...
.success(function(response) {
$scope.config = [];
$scope.config.machine_width = machineLength.x = parseFloat(response.config.machine_width);
angularFinishedJob(); // call the script in the main page when job is done
...
主页:
<script>
console.log(machineLength); // empty object, because angular job didn't finish
function angularFinishedJob(){ // called from angularApp.js
console.log(machineLength); // calculated object
// add code which uses machineLength
}
</script>
控制台日志(机器长度);//空对象,因为角度作业未完成
从angularApp.js调用函数angularFinishedJob(){//
console.log(machinelelength);//计算对象
//添加使用machineLength的代码
}
注意:如果您有权修改
angularApp.js
,我认为这将是最容易实现的方法。如果没有,则存在其他解决方案注入$window并使用该解决方案:
let sliceApp = angular.module('sliceConfig', []);
sliceApp.
.controller('sliceConfigController', function($window, $scope, $http)
{
$http.get("http://xxxxx/getSliceConfig")
.success(function(response)
{
$scope.config = [];
//$scope.config.machine_width = machineLength.x = parseFloat(response.config.machine_width);
$scope.config.machine_width = $window.machineLength.x = parseFloat(response.config.machine_width);
...
}
});
我不建议使用全局对象或$rootScope
来存储值,但它可以工作
相反,我建议使用值提供程序:
let sliceApp = angular.module('sliceConfig', []);
sliceApp.value("machineLength", {});
sliceApp.
.controller('sliceConfigController', function(machineLength, $scope, $http)
{
$http.get("http://xxxxx/getSliceConfig")
.success(function(response)
{
$scope.config = [];
$scope.config.machine_width = machineLength.x = parseFloat(response.config.machine_width);
...
}
});
注入$window并使用它:
let sliceApp = angular.module('sliceConfig', []);
sliceApp.
.controller('sliceConfigController', function($window, $scope, $http)
{
$http.get("http://xxxxx/getSliceConfig")
.success(function(response)
{
$scope.config = [];
//$scope.config.machine_width = machineLength.x = parseFloat(response.config.machine_width);
$scope.config.machine_width = $window.machineLength.x = parseFloat(response.config.machine_width);
...
}
});
我不建议使用全局对象或$rootScope
来存储值,但它可以工作
相反,我建议使用值提供程序:
let sliceApp = angular.module('sliceConfig', []);
sliceApp.value("machineLength", {});
sliceApp.
.controller('sliceConfigController', function(machineLength, $scope, $http)
{
$http.get("http://xxxxx/getSliceConfig")
.success(function(response)
{
$scope.config = [];
$scope.config.machine_width = machineLength.x = parseFloat(response.config.machine_width);
...
}
});
角度。价值可能help@A.J请提供更多信息,如何使用angular.value?谢谢,我现在无法使用笔记本电脑,我可能会在一小时后发布一个例子。谢谢(现在用手机交流);)先谢谢你。我在等你的信息help@A.J请提供更多信息,如何使用angular.value?谢谢,我现在无法使用笔记本电脑,我可能会在一小时后发布一个例子。谢谢(现在用手机交流);)先谢谢你。我在等你的信息。是的,这适用于console.log(machineLength)。但实际上这里有很多代码渲染场景,使用machineLength。如何在指定的machineLength之前阻止渲染?将该代码添加到
angularFinishedJob()
,或调用从angularFinishedJob()进行渲染的函数。是的,这适用于console.log(machineLength)。但实际上这里有很多代码渲染场景,使用machineLength。如何在指定machineLength之前阻止渲染?在angularFinishedJob()
中添加该代码,或从angularFinishedJob()
调用正在渲染的函数。如果在后续渲染中使用init值,machineLength是渲染区域的宽度、高度。长度。场景将使用init值渲染初始场景,然后当$http.get返回时,我必须重新渲染场景。这是为了回答您的问题,当您希望跨控制器访问变量时。但是您的用例是不同的。对于您的要求,我宁愿使用$emit和$on-watch表达式。谢谢您的时间和信息。如果在后续渲染中使用init值,machineLength是渲染区域的宽度和高度。长度。场景将使用init值渲染初始场景,然后当$http.get返回时,我必须重新渲染场景。这是为了回答您的问题,当您希望跨控制器访问变量时。但是您的用例是不同的。对于您的要求,我宁愿使用$emit和$on-watch表达式。谢谢您的时间和信息。你能多介绍一下吗。