在自定义指令中提供ngModel

providing ngModel into custom directive

本文关键字:ngModel 自定义 指令      更新时间:2023-09-26

我正在创建一个自定义的AngularJS指令,它使用带有更多特性的input标签。

index . html

sabel-input-directive ng-model="number" label="number" type='number' min="3" max="10" required="false" sabel-input-directive

我想提供ng-model属性,我可以使用它作为常规。

这个自定义标记只包含一个输入标记<input name='myInput'>

我试图在指令属性中传递字符串,并在内部标记中给予它,如:

input name='myInput' ng-model='{{theModel}}',但没有工作。

我知道我必须"require: ngModel"

但我不知道如何做到这一点:(和重要的事情,我想从自定义指令的属性指定模型名称:)

所以请帮助,因为这个标签将帮助很多我想!谢谢:)

ng-model属性中不能将themodel包裹在小胡子中

我想你错过了一篇关于指令作用域的好文章。链接到文章

从Angular控制器中获取ng-model和具有自己作用域的指令

angular.controller("ctr", function($scope){
  $scope.model = {
    name: "Hi"
  };
});
angular.directive("example", function(){
  return {
    restrict: "E",
    template: "<input type='text' ng-model='model'>",
    scope: {
      model: "="
    },
});

现在的html代码

<div ng-controller="ctr">
  <example data-model="model"></example>
</div>