更改离子列表项目单击上的CheckBox状态?

时间:2018-07-30 04:53:02

标签: ionic3 ion-checkbox

我在每行中都有一个列表和一个复选框。我希望每当我单击一个复选框时,对象都需要根据复选框状态进行相应的更新,但是当我在复选框上点击时使用下面的内容,却遇到无法设置属性“ 已选中”的异常未定义或空引用

.ts:

export class Custom{
  name: string
  empoloyeeID: number
  checked:boolean
}

export class CheckboxListPage {

  contacts:Array<Custom> = [];
  constructor(public navCtrl: NavController, public navParams: NavParams) {

    let customObj1 = new Custom();
    customObj1.empoloyeeID = 1;
    customObj1.name = "Ramakrishna"; 

    let customObj2 = new Custom();
    customObj2.empoloyeeID = 2;
    customObj2.name = "Ramakrishna2"; 

    this.contacts.push(customObj1);
    this.contacts.push(customObj2);

  }

  updateCucumber(contact){
        this.contacts[contact.id].checked = !contact.checked;
  }

.html:

<ion-content padding>
  <ion-list>
        <ion-item *ngFor="let contact of contacts" (click)="update(contact)">
            <!-- <ion-avatar item-start>
              <img src="imgs/img_snow.jpg">
            </ion-avatar> -->
            <ion-avatar item-left>
                <img src="https://ionicframework.com/dist/preview-app/www/assets/img/marty-avatar.png">
              </ion-avatar>
             <h2>{{contact.name}}</h2>
            <p *ngIf="contact.id===1;else other_content">your if block</p>
            <ng-template #other_content><p>your else block1</p></ng-template>

            <ion-row>
                <ion-col col-2 no-padding no-margin>
                  <ion-item no-padding no-margin no-lines>
                    <ion-checkbox [(ngModel)]="contact.checked" (ionChange)="updateCucumber(contact)"></ion-checkbox>
                  </ion-item>
                </ion-col>
                <ion-col col-10 no-padding no-margin>
                  <ion-item no-padding no-margin no-lines>
                    Agree to <a target="_blank" href="http://www.terms-of-service.com">Terms of Service</a>
                     and <a target="_blank" href="http://www.privacy-policy.com">Privacy Policy</a>.
                  </ion-item>
                </ion-col>
              </ion-row>

          </ion-item>


</ion-list>
</ion-content>

1 个答案:

答案 0 :(得分:0)

不确定我是否正确,但是如果您想在每次单击复选框时更新相应的联系人对象,则可以尝试如下操作:

<!DOCTYPE html>
<html ng-app="app">

<head>
  <script data-require="angular.js@*" data-semver="4.0.0" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.10/angular.min.js"></script>
  <script data-require="angular.js@*" data-semver="4.0.0" src="script.ts"></script>
  <script data-require="angular.js@*" data-semver="4.0.0" src="system.config.js"></script>
  <script data-require="angular.js@*" data-semver="4.0.0" src="tsconfig.json"></script>
  <link rel="stylesheet" href="style.css" />
  <script src="script.js"></script>
</head>

<body ng-controller="myctrl">

  <div id="wrapper" class="wrapper">

    <div id="aside" class="sidebar">
      <div>
        <ul class="nav">
          <li><a href="#">Home</a></li>
          <li><a href="#">Experience is the key of the item of the rest of the kind</a></li>
          <li><a href="#">Services</a></li>
          <li><a href="#">Bio</a></li>
          <li><a href="#">Contact</a></li>
        </ul>
      </div>
    </div>
    <!-- /Aside -->
    <div class="hideButton">
      <div class="Aligner">
        <a class="button" ng-click="toggle()"></a>
      </div>
    </div>
    <div id="content" class="content">
      <h1>Flexbox Off-Canvas Side Menu</h1>
      <h2>Easy to use</h2>
      <div>
        What is Lorem Ipsum? Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type
        specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more
        recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum. Why do we use it? It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The
        point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem
        Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like). Where
        does it come from? Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College
        in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and
        1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit
        amet..", comes from a line in section 1.10.32. The standard chunk of Lorem Ipsum used since the 1500s is reproduced below for those interested. Sections 1.10.32 and 1.10.33 from "de Finibus Bonorum et Malorum" by Cicero are also reproduced in
        their exact original form, accompanied by English versions from the 1914 translation by H. Rackham. Where can I get some? There are many variations of passages of Lorem Ipsum available, but the majority have suffered alteration in some form, by
        injected humour, or randomised words which don't look even slightly believable. If you are going to use a passage of Lorem Ipsum, you need to be sure there isn't anything embarrassing hidden in the middle of text. All the Lorem Ipsum generators
        on the Internet tend to repeat predefined chunks as necessary, making this the first true generator on the Internet. It uses a dictionary of over 200 Latin words, combined with a handful of model sentence structures, to generate Lorem Ipsum which
        looks reasonable. The generated Lorem Ipsum is therefore always free from repetition, injected humour, or non-characteristic words etc.
      </div>

    </div>
    <!-- /Content -->

  </div>
</body>

</html>

您的HTML几乎保持不变:

export class CheckboxListPage {
  contacts = [
    {
      name: "Ramakrishna",
      id: 1,
      selected: false
    },
    {
      name: "Ramakrishna2",
      id: 2,
      selected: false
    }
  ]

  constructor(public navCtrl: NavController,
    public navParams: NavParams) {}

  updateCucumber(contact) {
    contact.selected = !contact.selected;
  }
}
相关问题