AngularJS - 简单$范围的console.log()返回undefined [英] AngularJS - Simple $scope console.log() returns undefined
问题描述
我试图做一个简单的的console.log()
此范围$
I'm trying to make a simple console.log()
from this $scope:
<div ng-controller="CustomerController" id="customer-block">
<h3>Customer Information</h3>
<div class="col-md-4">
<label>Address 1:</label>
<input type="text" ng-model="customer.address1" class="form-content"
id="customer-address1" />
</div>
<div class="col-md-4">
<label>Address 2:</label>
<input type="text" ng-model="customer.address2" class="form-content"
id="customer-address2" />
</div>
<div class="col-md-4">
<label>City</label>
<input type="text" ng-model="customer.city" class="form-content"
id="customer-city" />
</div>
</div>
这是我的JavaScript文件:
This is my javascript file:
lima3app.controller("CustomerController", function($scope){
console.log($scope.customer);
});
但日志返回我的未定义。这有什么错呢?
这是plunkr: http://plnkr.co/edit/MU2i46o03bs22Jwh6QIe
推荐答案
由于其他人说,你需要初始化客户对象。
As the others have said, you need to initialize the customer object.
由于没有从控制器没有客户组值,则出现在视图中为未定义。当您在输入框中输入值,这将不再是不确定的,但由于记录只进行一次最初,在输入框中键入值没有效果。
Since there is no value of customer set from controller, it is appearing as undefined in the view. When you enter values in the input boxes, this will no longer be undefined, but since logging is done only once initially, typing values in input box has no effect
下面是我的script.js已经改变了部分
Here is the part which I have changed in script.js
lima3app.controller("CustomerController", function($scope){
$scope.customer = {
address1 : 'address1',
address2 : 'address2',
city:'city'
}
console.log($scope.customer);
});
这篇关于AngularJS - 简单$范围的console.log()返回undefined的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!