2017-11-28 10 views
0

皆さん。 今日、私はAngularJSについて何かを学んでいます。 私のプロジェクトでは、コントローラを構文として使用しようとしました。AngularJS結合変数

app.js

"use strict"; 

angular.module('myApp',[]); 

angular.module('myApp').controller('ParentController', [function(){ 
this.message = 'Hello from the parent.'; 
}]); 

angular.module('myApp').controller('FirstChild', [function(){ 
this.message = 'Hello from the first child.'; 
}]); 

angular.module('myApp').controller('SecondChild', ['$interval','$scope',function($interval, $scope){ 
this.message = 'Hello from the second child'; 
this.value = 1; 

$interval(function() { 
    this.value = Math.round(Math.random()*1000000)+1; 
}.bind(this),2000); 

$scope.$watch('second.value', function(newValue, oldValue){ 
    console.log('New', newValue, 'Old:', oldValue); 
}); 
}]); 

Index.htmlと

<!DOCTYPE html> 
<html> 
<head> 
<title>Controllers as syntax</title> 
<meta charset="ISO-8859-1"> 
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.6/angular.min.js"></script> 
<script type="text/javascript" src="app.js"></script> 
</head> 
<body> 

<div ng-controller="ParentController as parent"> 

<p>This is the parent: {{parent.message}}</p> 

<div ng-controller="FirstChild as first"> 
    <p>This is the parent: {{parent.message}}</p> 
    <p>This is the first child: {{first.message}}</p> 
</div> 

<div ng-controller="SecondChild as second"> 
    <p>This is the parent: {{parent.message}}</p> 
    <p>This is the secondChild:{{second.message}}</p> 
    <p>Randomize:{{second.value}}</p> 
</div>  
</div> 

</body> 
</html> 

私はページをロードしようとすると、これは私の出力である:

これは親です:{{parent.message }}

これは親です:{{parent.message}}

これは最初の子である:{{first.message}}

これは親である:{{parent.message}}

これはsecondChildある:{{second.message}}

ランダム化:{{second.value}}

どうすれば修正できますか?

+1

は '置く' NG-app' –

+1

'NG-アプリ= "MyApp]を" 忘れてしまったはず不足していますhtmlタグに置く –

+1

[OK](https://stackoverflow.com/hel)に役立つ回答を記入してくださいp/accepted-answer)を入力してください。 – Melebius

答えて

0

あなたはNG-アプリ

<body ng-app="myApp"> 

DEMO

"use strict"; 
 

 
angular.module('myApp',[]); 
 

 
angular.module('myApp').controller('ParentController', [function(){ 
 
this.message = 'Hello from the parent.'; 
 
}]); 
 

 
angular.module('myApp').controller('FirstChild', [function(){ 
 
this.message = 'Hello from the first child.'; 
 
}]); 
 

 
angular.module('myApp').controller('SecondChild', ['$interval','$scope',function($interval, $scope){ 
 
this.message = 'Hello from the second child'; 
 
this.value = 1; 
 

 
$interval(function() { 
 
    this.value = Math.round(Math.random()*1000000)+1; 
 
}.bind(this),2000); 
 

 
$scope.$watch('second.value', function(newValue, oldValue){ 
 
    console.log('New', newValue, 'Old:', oldValue); 
 
}); 
 
}]);
<!DOCTYPE html> 
 
<html> 
 
<head> 
 
<title>Controllers as syntax</title> 
 
<meta charset="ISO-8859-1"> 
 
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.6/angular.min.js"></script> 
 
<script type="text/javascript" src="app.js"></script> 
 
</head> 
 
<body ng-app="myApp"> 
 

 
<div ng-controller="ParentController as parent"> 
 

 
<p>This is the parent: {{parent.message}}</p> 
 

 
<div ng-controller="FirstChild as first"> 
 
    <p>This is the parent: {{parent.message}}</p> 
 
    <p>This is the first child: {{first.message}}</p> 
 
</div> 
 

 
<div ng-controller="SecondChild as second"> 
 
    <p>This is the parent: {{parent.message}}</p> 
 
    <p>This is the secondChild:{{second.message}}</p> 
 
    <p>Randomize:{{second.value}}</p> 
 
</div>  
 
</div> 
 

 
</body> 
 
</html>

関連する問題