javascript - Angular : Wrapping an element into a custom template with an angular attribute directive

标签 javascript angularjs angularjs-directive angular-ngmodel transclusion

情况:

我有一个属性指令,将它的元素包装到一个模板中。在这里:

app.directive("myCustomInput", function(){
  return{
    restrict: "A",
    replace: true,
    scope:{},
    transclude: "element",
    template: "<div class='input-wrap'>"+
    "<div ng-transclude></div>"+
    "<i class='glyphicon glyphicon-chevron-down'></i>"+
    "</div>"
  }
});

我这样使用它:

<input my-custom-input ng-model="data.input" type="text" />

问题:

ng-model 不起作用

这是plunker

最佳答案

您可能遇到了一个可能的错误。这是一个优先级和指令性的处理顺序问题。将您的指令设置为比 ng-model 更高的优先级。使用 1.2 v 时,ng-model 的默认优先级为 0,而 1.3 版本 ng-model 的优先级为 1。因此,让您的指令具有比 ng-model 更高的优先级,以便指令和包含发生在 ng-model 处理指令呈现之前的输入之前。

.directive("myCustomInput", function(){
  return{
    restrict: "A",
    replace: true,
    scope:{},
    priority: 1, //Here set the priority
    transclude: "element",
    template: "<div class='input-wrap'>"+
    "<div ng-transclude></div>"+
    "<i class='glyphicon glyphicon-chevron-down'></i>"+
    "</div>"
  }
});

Demo

关于javascript - Angular : Wrapping an element into a custom template with an angular attribute directive,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27650044/

相关文章:

javascript - AngularJS 1.3.13 - 传递给 UI-Router 配置的服务提供者未解析

javascript - 指令 "link"不符合我的要求

javascript - 重复和插入指令

javascript - 未加载 Angular 指令 html(控制台中没有错误)

javascript - 使用原型(prototype)/scriptactulous 样式/替换选择框

javascript - 如何在 Javascript 中并行运行 async/await

javascript - Angular 工厂访问私有(private)函数

javascript - Protractor 单击链接并将服务器响应与文件进行比较

javascript - 管理旋转元素上的拖动

javascript - 仅当使用 jquery 未正确输入用户名或密码时,如何才能显示错误消息?