[英]When/how often does code run in Javascript
I'm just trying different things to learn how javascript works, and I'm wondering why the following doesn't work.我只是在尝试不同的事情来学习 javascript 的工作原理,我想知道为什么以下内容不起作用。
I've got a function that toggles independent_color
between "blue"
and "red"
on button click:我有一个功能,可以在单击按钮时在"blue"
和"red"
之间切换independent_color
"blue"
:
$scope.independent_color = "blue";
$scope.toggle_independent_color = function() {
if ($scope.independent_color == "blue") {
$scope.independent_color = "red"
}
else {
$scope.independent_color = "blue"
}
}
Then, I have some conditionals that set dependent_color
based on the value of independent_color
:然后,我有一些条件根据independent_color
的值设置dependent_color
:
if ($scope.independent_color == "blue") {
$scope.dependent_color = "blue"
}
else {
$scope.dependent_color = "red"
}
However, when I toggle independent_color
, the value of dependent_color
does not change.但是,当我切换independent_color
, dependent_color
的值不会改变。
Here's a FIDDLE: https://jsfiddle.net/exr3tqm0/5/这是一个小提琴: https ://jsfiddle.net/exr3tqm0/5/
My question is not how to get this to work.我的问题不是如何让它发挥作用。 Rather, I'd like to know why it doesn't work.相反,我想知道为什么它不起作用。 When/how often does the code run?代码何时/多久运行一次?
It does not change because this code:它不会改变,因为此代码:
if ($scope.independent_color == "blue") {
$scope.dependent_color = "blue"
}
else {
$scope.dependent_color = "red"
}
lies directly in controller function, so it's executed only once, when controller is created (when you enter the view).直接在控制器函数中,所以它只执行一次,当控制器被创建时(当你进入视图时)。 Simply change your dependantColor
in toggle_independent_color
.简单地改变你的dependantColor
在toggle_independent_color
。 You can also handle this dependency using angular's $scope.watch
but it's not needed in your case.您也可以使用 angular 的$scope.watch
处理此依赖$scope.watch
但在您的情况下不需要它。
The code runs once.代码运行一次。 The function for toggle_independent_color
runs as many times as you click (your fiddle shows it attached via ng-click). toggle_independent_color
的函数在您单击时运行多次(您的小提琴显示它是通过 ng-click 附加的)。 All the other code runs once when your controller is instantiated.所有其他代码在您的控制器实例化时运行一次。 If you instantiate your controller multiple times (such as in having multiple of the same views) then it will run that many times, but each instantiated controller will have its own child scope.如果你多次实例化你的控制器(比如有多个相同的视图),那么它会运行很多次,但每个实例化的控制器都有自己的子作用域。
On a side note, to get it to work, you can try putting that dependent_color
functionality into another function.附带说明一下,要使其工作,您可以尝试将该dependent_color
功能放入另一个函数中。
var app = angular.module("myApp", []);
app.controller("myController", ["$scope", function($scope) {
$scope.independent_color = "blue";
$scope.toggle_independent_color = function() {
if ($scope.independent_color == "blue") {
$scope.independent_color = "red"
}
else {
$scope.independent_color = "blue"
}
checkDependent(); // check the dependent color when the button is clicked
}
// this check needs to be run each time the color is changed
function checkDependent(){
if ($scope.independent_color == "blue") {
$scope.dependent_color = "blue"
}
else {
$scope.dependent_color = "red"
}
}
}]);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.