我刚开始使用AngularJS 1.3。我试图访问我在函数内分配的范围相关属性。但似乎有些不对劲。有人可以帮帮我吗?
<!DOCTYPE html>
<html data-ng-app="">
<head>
<title>SimpleController</title>
</head>
<body>
<div data-ng-controller="SimpleController">
<ul>
<li data-ng-repeat="emp in employees">
{{emp.name}}
</li>
</ul>
</div>
<script src="angular.min.js"></script>
<script>
function SimpleController($scope) {
$scope.employees = [
{name : 'emp1', id : 'id1'},
{name : 'emp2', id : 'id2'},
{name : 'emp3', id : 'id3'}
];
}
</script>
</body>
</html>
答案 0 :(得分:2)
似乎问题是我使用的语法已被弃用且与Angular 1.3不兼容。这是正确的方法:
<!DOCTYPE html>
<html ng-app="myApp">
<head>
<title>SimpleController</title>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
</head>
<body ng-controller="SimpleController">
<ul>
<li data-ng-repeat="emp in employees">
{{emp.name}}
</li>
</ul>
<script>
angular.module('myApp', []).controller('SimpleController', function($scope){
$scope.employees = [
{'name' : 'emp1', 'id' : 'id1'},
{'name' : 'emp2', 'id' : 'id2'},
{'name' : 'emp3', 'id' : 'id3'}
];
});
</script>
谢谢大家的帮助!