ng-controller没有传递值来查看

时间:2015-03-18 06:30:14

标签: angularjs model-view-controller angular-ngmodel

<html>
<head>
<script src="angular.js"></script>
</head>
<body> 
<div ng-app="myapp1">
<div ng-controller="cont1">
    <h1>{{data.message}}</h1>
</div>
</div>
<script>
angular.module('myapp1', []).controller('cont1', function($scope){
$scope.data = "{message: 'Hello!'}";
});
</script>
</body> 
</html>

我从控制器传递值&#39; cont1&#39;以及'myapp1&#39;的观点模块。我应该看到结果为&#34;你好!&#34;在HTML页面上,但我得到纯白色的空白页面。有人可以帮我吗?

3 个答案:

答案 0 :(得分:1)

你所做的一切都是

$scope.data = "{message: 'Hello!'}";

应该是

$scope.data = {message: 'Hello!'};

这是DEMO

答案 1 :(得分:0)

当你将对象插入范围时,请不要使用(“”)。改变如下:

	
$scope.data = {message: 'Hello!'};

答案 2 :(得分:0)

正确格式化数据:

angular.module('myapp1', [])
 .controller('cont1', function($scope){
   $scope.data = {message: 'Hello!'};
 });
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

<div ng-app="myapp1">
  <div ng-controller="cont1">
    <h1>{{data.message}}</h1>
  </div>
</div>