在下面的代码中,
<!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 usingthis
instead of$scope
.Controllers
asclasses
, we areinitializing
them(var student = new Student()
) usingcontroller 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/