首页 > 关于angularjs双向绑定的问题

关于angularjs双向绑定的问题

我在html页面里定义了input的表签并且给定了一个ng-model="newLabel", 但为什么我在js里面用
$scope.newLabel获取不到值哦 一直是未定义。 js是外部引用的

这是控制器代码

app.controller('addLabelList',['$scope','severLabel',function ($scope,severLabel) {
  console.log($scope.selectSite);
  $scope.saveBtn = function () {
    var str = $scope.newLabel;

    console.log(str);
    var _newLabel = {name: str, percent: "100% "};
    console.log($scope.selectSite);
    severLabel.setName(_newLabel);
  }

}])

这是html代码

<ion-view view-title="添加标签" ng-controller="addLabelList">

  <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中可以获取到,但我现在必须要在外部引用呢


我找到答案了,在这里https://.com/q/1010000000666592 回答第一条


console.log($scope.newLabel);


ng–click="saveBtn(newLabel)"


自身语法错误?少了<ion-list type="list-inset " class="addLabel">的关闭标签


试下把数据绑定到一个对象上。比如用data.newLabel。

【热门文章】
【热门文章】