【问题标题】:binding data not working in angularjs绑定数据在angularjs中不起作用
【发布时间】:2014-12-16 01:45:03
【问题描述】:

我是 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'}
    ];
});

【问题讨论】:

  • 控制台中的任何错误
  • 如果您使用 JSFiddle 进行测试,请确保选择 No wrap - in &lt;head&gt; (Explanation)
  • 您的代码有效:jsfiddle.net/jwqnm05f 您可能忘记添加对 AngularJs 库的引用
  • @Josep 即使@Ashraf 确实使用了JSFiddle,默认是onLoad,这将不起作用。
  • @Mr.Polywhirl OP 没有提到任何关于 jsFiddle 的内容。我已经知道了,如果您查看我的 jsFiddle 示例,我已经在这样做了。

标签: javascript angularjs


【解决方案1】:

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

如何构造&lt;head&gt;

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

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

工作sn-p

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>

【讨论】:

  • 这个问题的答案?真的吗?
  • @Mr. Polywhirl 谢谢问题是js代码在body区
猜你喜欢
  • 2016-01-16
  • 2014-10-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-05-31
相关资源
最近更新 更多