<!DOCTYPE html>
<html ng-app="app">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title></title>
<link rel="stylesheet" href="../css/lib/bootstrap4.css">
<script type="text/javascript" src='../js/lib/angular.js' ></script>
<script type="text/javascript" src='js/initAndController.js'></script>
</head>
<body ng-init="foo='bar'">
<p ng-controller="myCtrl">
</p>
</body>
</html>
js
var app = angular.module('app', []);
app.controller('myCtrl', ['$scope', function($scope){
console.log(foo)
}])
为什么会报错,说foo
是undefined
?
另外下面这段代码也让我不解:
<!DOCTYPE html>
<html ng-app="app">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title></title>
<link rel="stylesheet" href="../css/lib/bootstrap4.css">
<script type="text/javascript" src='../js/lib/angular.js' ></script>
<script type="text/javascript" src='js/directive.js'></script>
</head>
<body ng-controller="myCtrl">
<my-directive />
<h1 ng-init="greet='hello world'">
The greeting is {{greet}}
</h1>
</body>
</html>
js
var app = angular.module('app', []);
app.controller('myCtrl', ['$scope', function($scope){
}])
app.directive('myDirective',function(){
return {
restrict : 'E',
replace : true,
template : '<a href="http://google.com">to Google</a>'
}
})
为什么结果只有一个to Google而没有h1标签里的文字?
첫 번째: console.log(foo)
console.log($scope.foo)여야 합니다
두 번째: replacement:true 속성의 의미를 이해하세요
ng-init가 ng-controller="myCtrl" 외부에 정의되어 있기 때문입니다. 모든 컨트롤러가 초기화될 때까지 ng-init가 실행되지 않을 것 같아서 현재로서는 foo가 정의되지 않았습니다
이 질문은 명확하지 않습니다.