开发者社区> 问答> 正文

关于angularjs双向绑定的问题

screenshot

  <ion-nav-buttons side="left">
    <a class="button button-icon icon ion-ios-undo-outline" href="#/tabs/label"></a>
  </ion-nav-buttons>

  <ion-nav-buttons side="right">
    <button class="button button-clear" style="font-size: 11pt;padding-right: 6px" ng-click="saveBtn()" ui-sref="tabs.label">
      保存
    </button>
  </ion-nav-buttons>
  <ion-content>
    <ion-list type="list-inset " class="addLabel">
      <ion-item>
        标签名
        <input type="text" placeholder="请输入标签" ng-model="newLabel" style="display: inline; padding-left: 20pt; font-size: 11pt">
      </ion-item>
      <ion-item class="item-select">
        掌握情况
        <select>
          <option>10%</option>
          <option>20%</option>
          <option>40%</option>
          <option>60%</option>
          <option>80%</option>
          <option selected="">100%</option>
        </select>
      </ion-item>
  </ion-content>
</ion-view>

我每次在js中输出model时候都显示undefined
请问可能什么原因呢? 对了 我在别的地方试,将js写在html中可以获取到,但我现在必须要在外部引用呢

展开
收起
杨冬芳 2016-06-13 17:22:43 2151 0
1 条回答
写回答
取消 提交回答
  • IT从业

    我怀疑你这是被 scope 的原型继承坑了

    像 ion-content 这些指令都是有各自的 scope 的,然后你在视图里写上 ng-model="myMessage" ,其实你在输入框填入的内容是放到了 ion-item 的 scope 上了,而你的 roomCtrl 的 scope 里的 myMessage 依旧是 undefined ;而当你在控制器里给 myMessage 赋值完了以后,由于 ion-item 的 scope 上还没有 myMessage 属性,所以就会从原型链上找,进而找到了 roomCtrl 的 scope 上的 myMessage 。

    这是我常用的解决方案:

    $scope.ctrlScope = $scope

    全选复制放进笔记

    2019-07-17 19:36:33
    赞同 展开评论 打赏
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
Angular从零到一 立即下载
低代码开发师(初级)实战教程 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载