绑定数据在angularjs中不起作用

时间:2014-10-20 23:14:18

标签: javascript angularjs

我是AngularJS的新手,我正在尝试将数据绑定到表单控制器,但它无法正常工作。

HTML

<div ng-app="myapp"  data-ng-controller="simpleContr">
    <ul>
        <li data-ng-repeat="user in users">
            {{ user.name }}
        </li>
    </ul>
</div>   

AngularJS

angular.module('myapp', []).controller('simpleContr', function($scope) {
    $scope.users = [
        { name:'Ashraf', city:'Cairo'},
        { name:'Ali', city:'Assuit'},
        { name:'Mohammed', city:'Qena'}
    ];
});

1 个答案:

答案 0 :(得分:0)

以下工作如下,因为javascript被插入到输出文档的头部。

如何构建<head>

<head>
    <script type="text/javascript" src="angular.js"></script>

    <script type="text/javascript">
        // Your controller code...
    </script>
</head>

工作代码段

angular.module('myapp', []).controller('simpleContr', function ($scope) {
    $scope.users = [{
        name: 'Ashraf',
        city: 'Cairo'
    }, {
        name: 'Ali',
        city: 'Assuit'
    }, {
        name: 'Mohammed',
        city: 'Qena'
    }];
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

<div ng-app="myapp" data-ng-controller="simpleContr">
    <ul>
        <li data-ng-repeat="user in users">{{ user.name }}</li>
    </ul>
</div>