AngularJS - 简单$ scope console.log()返回undefined [英] AngularJS - Simple $scope console.log() returns undefined

查看:1866
本文介绍了AngularJS - 简单$ scope console.log()返回undefined的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从这个$ scope中创建一个简单的 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);

});

但日志会返回未定义。这是什么问题?

But the log returns me undefined. What's wrong with that?

这是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

Plunker演示

这是我在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 - 简单$ scope console.log()返回undefined的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆