【问题标题】:ngTagsInput not binding to modelngTagsInput 未绑定到模型
【发布时间】:2017-10-13 14:12:12
【问题描述】:

我刚开始使用 ngTagsInput 角度库,但在更新模型时遇到了问题。我在 ionicPopup 中显示标签输入,有趣的是,添加标签的事件会触发,这表明 $scope.sites[] 已填充。

当我调用 $scope.test() 时,数组为空。它保存值的唯一上下文是与添加标签事件相关联的方法。

这是一个简化的控制器示例:

$scope.sites = [];

$scope.addSites = function() {
    // this works
    console.log("In addSites");
    console.dir($scope.sites);
}

$scope.test = function() {
  // in any other method $scope.sites is empty
  console.dir($scope.sites);
}

在我看来,我将标签元素定义为:

<tags-input ng-model="sites" add-on-space="true" placeholder="Add Site Numbers" on-tag-added="addSites($tag)"></tags-input>

我觉得非常奇怪的是,$scope 变量可以包含我期望在库事件上下文中的值,但不能包含在该上下文之外。

在绑定方面有什么特别需要做的吗?就文档而言,我看不到我缺少什么。任何建议将不胜感激。

【问题讨论】:

    标签: javascript angularjs ionic-framework ng-tags-input


    【解决方案1】:

    经过大量试验和错误,我最终通过执行以下操作解决了这个问题。

    <tags-input ng-model="$parent.sites" add-on-space="true" placeholder="Add Site Numbers" on-tag-added="addSites($tag)"></tags-input>
    

    我已经在对 $ionicPopup({}) 的调用中传递了 'scope : $scope' 但是没有将 ng-model 设置为 "$parent.sites" 它没有正确绑定。

    希望这可以避免有人在路上拔头发!

    【讨论】:

      猜你喜欢
      • 2021-07-21
      • 2013-02-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-20
      • 2012-06-18
      • 2014-04-10
      • 1970-01-01
      相关资源
      最近更新 更多