简体   繁体   English

Angularjs数据绑定失败并提前进行了Angular UI-Bootstrap

[英]Angularjs data-binding fails with Angular UI-Bootstrap typeahead

I have form field for a website address url that feeds a display elsewhere on the page with ng-model data-binding (it simply shows the url, like " http://thiswebsite.com "). 我有一个网站地址网址的表单字段,该网址使用ng-model数据绑定在页面上的其他地方进行显示(它只是显示该网址,例如“ http://thiswebsite.com ”)。 I have another form field that I'm using the angular ui.bootstrap typeahead directive on. 我还有另一个表单字段,我正在使用angular ui.bootstrap typeahead指令。 The typeahead functionality itself is working fine, however when typeahead is activated (when I begin typing in the typeahead field) then the website address display turns into a hyperlink and it's data-binding fails- meaning if I try typing in the website address url field again the url display doesn't update. 提前输入功能本身可以正常工作,但是,当提前输入功能被激活时(当我开始在提前输入字段中键入内容时),则网站地址显示变为超链接,并且数据绑定失败-这意味着如果我尝试在网站地址url字段中输入网址显示也不会更新。

Only the typeahead field in the form triggers the issue and only displays fed from the form that contain urls are affected. 仅表单中的typeahead字段会触发问题,并且仅表单中包含url的显示会受到影响。 I've tried changing the input type for the website address url input field from 'type="url"' to 'type="text"' but no dice. 我尝试将网址url输入字段的输入类型从'type =“ url”'更改为'type =“ text”',但没有骰子。

I put together a jsfiddle but it doesn't reproduce the problem, I presume because I have it set up with older dependency versions (the fiddle is set up with ui.bootstrap 0.4.0 and angular 1.0.7). 我把一个jsfiddle放在一起,但是它不能重现这个问题,我想是因为我用较早的依赖版本进行了设置(该小提琴是使用ui.bootstrap 0.4.0和angular 1.0.7设置的)。 I set up the fiddle like this because when I tried to set up the fiddle with the dependency versions I'm using (angular 1.3.3 and ui.bootstrap 0.12.0) it wouldn't work at all (data-binding and typeahead both failed outright). 我这样设置小提琴,是因为当我尝试使用我使用的依赖项版本(角度1.3.3和ui.bootstrap 0.12.0)设置小提琴时,它根本无法工作(数据绑定和预输入)都彻底失败了)。 But maybe the fiddle will help anyway. 但是也许小提琴还是会有所帮助的。

Relevant code: 相关代码:

HTML: HTML:

<div ng-controller="mainCtrl">
   <form role="form">
      <div class="form-group">
         <label>Website Address:</label>
         <input class="form-control" type="text" ng-model="provider.website">
      </div>
      <div class="form-group">
         <label>Service Types (ADD):</label>
         <input typeahead="service for service in allServiceTypesArray | filter:$viewValue | limitTo:8" typeahead-editable="false" typeahead-on-select="addServiceType($item, $model, $label)" ng-model="serviceTypeInput" class="form-control" type="text">
      </div>
   </form>
   <table id="provider-table">
      <tr>
         <td class="tableLabels">Website address display:</td>
         <td class="tableContents">{{provider.website}}</td>
      </tr>
      <tr>
         <td class="tableLabels">Provider Type(s):</td>
         <td class="tableContents">{{serviceTypesDisplay}}</td>
      </tr>
   </table>
</div>

JS: JS:

angular.module('myApp', ['ui.bootstrap'])
.controller("mainCtrl", function ($scope) {

  $scope.allServiceTypesArray = [ "cleaning"
                                , "mop the floor"
                                , "answer the door"
                                , "superman no home" ]

  $scope.provider = {}

  clearProviderForm();

  function clearProviderForm() {
    $scope.provider.website = ''
    $scope.provider.serviceTypes = [];
    $scope.serviceTypesDisplay = '';
  }

  // add selected service types to array for "Service Types" input field
  $scope.addServiceType = function ($item, $model, $label) {
    $scope.provider.serviceTypes.push($model);
    $scope.provider.serviceTypes.sort();
    $scope.serviceTypeInput = '';
    $scope.serviceTypesDisplay = $scope.provider.serviceTypes.join(", ");
  };
});

I've scoured SO, googled the heck out of this, and combed the UI-Bootstrap github issues but it appears I'm the only person in the world that has run into this (or it's some insanely simple thing I'm doing wrong!). 我已经搜索过了,搜索了一下这个问题,并梳理了UI-Bootstrap github问题,但看来我是世界上唯一遇到这个问题的人(或者这是我做错的非常简单的事情!)。 It may very well be just a version incompatibility issue but I'd like to know in case it's not, and if it is perhaps somebody knows a fix or even an acceptable work-around. 它很可能只是版本不兼容的问题,但我想知道是否是这样,如果不是的话,也许有人知道解决方法甚至可以接受的解决方法。

Of course any help will be greatly appreciated. 当然,任何帮助将不胜感激。

Got it! 得到它了! I'm not sure what caused the problem but I found a solution: 我不确定是什么引起了问题,但我找到了解决方案:

Instead of double curly-braces: 代替双花括号:

<td class="tableContents">{{provider.website}}</td>

Going with ng-bind: 使用ng-bind:

<td class="tableContents" ng-bind="provider.website"></td>

solved the problem. 解决了问题。 The website addresses still show as hyperlinks but data-binding works as it should when typing in the input field. 网站地址仍显示为超链接,但在输入字段中键入数据时,数据绑定应按其应有的方式工作。

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

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