您好我正在学习角度并尝试将模型中的数据绑定到视图。但它没有抛出任何错误。你能帮帮我吗?
<div ng-app ="myApp" ng-controller="emp">
Hello {{emp.name}}, I am happy that you are getting a salary of{{emp.salary}}.
</div>
var app = angular.module("myApp", []);
app.controller('emp', function(){
this.name = "John";
this.salary = 4500;
});
请找Fiddler
答案 0 :(得分:3)
将ng-controller="emp"
更改为ng-controller="emp as emp"
<div ng-app ="myApp" ng-controller="emp as emp">
Hello {{emp.name}}, I am happy that you are getting a salary of{{emp.salary}}.
</div>
答案 1 :(得分:2)
您应该在这里使用 controller as syntax
,
更改为ng-controller="emp as emp"
<强>样本强>
var app = angular.module("myApp", []);
app.controller('emp', function(){
this.name = "John";
this.salary = 4500;
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app ="myApp" ng-controller="emp as emp">
Hello {{emp.name}}, I am happy that you are getting a salary of{{emp.salary}}.
</div>
答案 2 :(得分:0)
您缺少$scope
var app = angular.module("myApp", []);
app.controller('emp', ['$scope', function($scope) {
$scope.empData = {
name: "John",
salary: 4500
}
}]);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp" ng-controller="emp">
Hello {{empData.name}}, I am happy that you are getting a salary of{{empData.salary}}.
</div>