我正在尝试通过此$ scope创建一个简单的console.log():

<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文件:
lima3app.controller("CustomerController", function($scope){

  console.log($scope.customer);

});

但是日志返回 undefined 。怎么了

这是个笨蛋:http://plnkr.co/edit/MU2i46o03bs22Jwh6QIe

最佳答案

正如其他人所说,您需要初始化客户对象。

由于没有来自 Controller 的客户设置值,因此它在 View 中显示为未定义。当您在输入框中输入值时,将不再是未定义的,但是由于最初只记录一次日志,因此在输入框中键入值无效

Plunker Demo

这是我在script.js中更改的部分

lima3app.controller("CustomerController", function($scope){

$scope.customer = {
  address1 : 'address1',
  address2 : 'address2',
  city:'city'
}

console.log($scope.customer);

});

关于AngularJS-简单的$ scope console.log()返回未定义,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/24901339/

10-13 09:34