简体   繁体   English

Angular-Beginner NgModel绑定

[英]Angular-beginner NgModel binding

Beginner-level question: 入门级问题:

I have a simple input type=text with ng-model="xyz.zyx" , where xyz is an object. 我有一个简单的input type=text带有ng-model="xyz.zyx" ,其中xyz是一个对象。 In the controller, I create that object and assign a value to zyx property with the following assignment: 在控制器中,我创建该对象,并通过以下分配为zyx属性分配值:

xyz {
  zyx: $scope.zzz
}

However, there are no input fields on the page with ng-model="zzz" , and yet, zyx is being assigned to the value of the input field that I described in the beginning, the one with ng-model="xyz.zyx" . 但是,页面上没有ng-model="zzz"的输入字段,但是, zyx被分配给了我开头所描述的输入字段的值,即ng-model="xyz.zyx"的输入字段ng-model="xyz.zyx" How come this is happening? 怎么会这样呢? Where is $scope.zzz coming from? $scope.zzz来自哪里?

I put in a comment to your question but I also wanted to put together a plunker to help you tinker with what's going on. 我对您的问题发表了评论,但我也想组装一个塞子,以帮助您修改正在发生的事情。

http://plnkr.co/edit/vYI1XiSm4cnEJjLRSO85 http://plnkr.co/edit/vYI1XiSm4cnEJjLRSO85

js js

var app = angular.module('app', []);

app.controller('MainCtrl', MainCtrl);
MainCtrl.$inject = ['$scope'];

function MainCtrl ($scope) {
  $scope.zzz = "Test";

  $scope.xyz = {
    zyx: $scope.zzz
  }
}

html html

<html ng-app="app">

  <head>
    <meta charset="utf-8" />
    <title>AngularJS Plunker</title>
    <link rel="stylesheet" href="style.css" />
    <script data-require="angularjs_1_3_15@*" data-semver="1.3.15" src="https://code.angularjs.org/1.3.15/angular.min.js"></script>
    <script src="app.js"></script>
  </head>

  <body ng-controller="MainCtrl">
    <input type="text" ng-model="xyz.zyx">
    <pre>zzz: {{zzz | json}}</pre>
    <pre>xyz: {{xyz | json}}</pre>
  </body>

</html>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM