为什么以下代码片段在Angular JS中有效?
var app = angular.module('store',[]);
(function(){
app.controller('StoreController',function(){
this.blabla = student;
});
})();
var student =
{
name:"Abc Def",
rollNumber:12,
isBrilliant: true,
isMale:false,
isFemale: false,
istest:true
};
Run Code Online (Sandbox Code Playgroud)
即使student
在使用它并且student
没有悬挂的功能之后仍然为什么上述功能仍然有效?
但与上面的例子相比,这个:
(function(){
console.log("Name is :"+student);
})();
var student = {
name:"xyz"
};
Run Code Online (Sandbox Code Playgroud)
显示student
作为undefined
意味着它没有悬挂.
app.js
是:
var app = angular.module('myApp',[]);
app.directive('myDirective2', function () {
return{
restrict: 'A',
priority: 100,
//template:"<h1>myDirective2</h1>",
controller: function ($scope, $element, $transclude,$timeout) {
//$scope.name = "executed myDirective2";
$timeout(function () {
$scope.name = "executed myDirective2";
}, 3000);
}
};
});
app.directive('myDirective3', function () {
return{
restrict: 'A',
priority: 200,
//template:"<h1>myDirective3</h1>",
controller: function ($scope, $element, $transclude, $timeout) {
$timeout(function () {
$scope.name = "executed myDirective3";
}, 3000);
}
};
});
Run Code Online (Sandbox Code Playgroud)
和 index.html
是:
<!DOCTYPE html>
<html ng-app="myApp">
<head>
<script src="js/angular.js" type="text/javascript"></script>
<script src="js/app.js" …
Run Code Online (Sandbox Code Playgroud) app2.js
(function(){
var app = angular.module("panel",[]);
app.controller('PanelController',function(){
this.tab = 1;
this.setTab = function(setTab){
this.tab = setTab;
};
});
})();
Run Code Online (Sandbox Code Playgroud)
而且view
是:
<html ng-app="panel">
<head>
<link rel="stylesheet" type="text/css" href="bootstrap.min.css">
<script type="text/javascript" src="angular.js"></script>
<script type="text/javascript" src="app2.js"></script>\
<style type="text/css">
li{
width:100px;
}
</style>
</head>
<body>
<section ng-controller="PanelController as panel">
<ul class="nav nav-pills" >
<li ng-class="{active:tab === 1}"><a ng-click="panel.setTab(1)" href="#">1</a></li>
<li ng-class="{active:tab === 2}"><a ng-click="tab = 2" href="#">2</a></li>
<li ng-class="{active:tab === 3}"><a ng-click="tab = 3" href="#">3</a></li>
<li ng-class="{active:tab === 4}"><a ng-click="tab = 4" …
Run Code Online (Sandbox Code Playgroud) 当我跑进去的时候 console
console.log("1");
setTimeout(function(){console.log("2");},3000);
console.log("3");
setTimeout(function(){console.log("4");},1000);
Run Code Online (Sandbox Code Playgroud)
我明白了:
3
之前和之后用蓝色写的数字是多少4
?