javascript - 数据未绑定(bind)在 View 中

标签 javascript angularjs

在下面的代码中,

<!DOCTYPE html>
    <html>
        <head>
            <meta charset="UTF-8">
            <title>Controller</title>
            <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.3.20/angular.js"></script>
            <script type="text/javascript">
                var app = angular.module("sample", []);
                app.controller("emp", function(){
                        this.Name = "jag";
                    });
            </script>
        </head>
        <body ng-app="sample">
            <div ng-controller="emp">
                Hello {{Name}}
            </div>

        </body>
    </html>
<小时/>

{{ Name }} 表达式不计算。

1) 如何检查 $scope 模型是否绑定(bind)到 View 中的 div 模板?

2) 我了解到作用域位于 DOM。使用指令 ng-controller="emp" 创建 Controller 实例。我可以通过 DOM 访问 Controller (emp)实例和$scope吗?

最佳答案

Use controller as if you are using this instead of $scope. Controllers as classes, we are initializing them(var student = new Student()) using controller as syntax.

var app = angular.module("sample", []);
app.controller("emp", function() {
  this.Name = "jag";
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

<body ng-app="sample">
  <div ng-controller="emp as e">
    Hello {{e.Name}}
  </div>

</body>

关于javascript - 数据未绑定(bind)在 View 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34701381/

相关文章:

javascript - 获取 $(this) 的选定选项

javascript - 动态获取查询值并在按钮单击 PHP 时触发

javascript - Angular 变量未在 HTML 中显示

javascript - Angular : How to call a function within factory?

c# - JavaScript 函数参数中的 '\' 是什么意思?

javascript - 我怎样才能得到一个节点的唯一直接子节点的特定类名?

javascript - Linux 操作系统上的 ibm_db 模块安装问题

javascript - 如何使用 AngularJS 上传到 Cloudinary?

android - 在 Ionic App 中通过手指(触摸)裁剪图像

javascript - AngularJS 按空格键调用 href (ui-router)