AngularJS - 创建一个使用NG-model的指令 [英] AngularJS - Create a directive that uses ng-model

查看:206
本文介绍了AngularJS - 创建一个使用NG-model的指令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个将创建一个输入字段具有相同NG-模型创建的指令元素指令。

I am trying to create a directive that would create an input field with the same ng-model as the element that creates the directive.

这就是我想出迄今:

HTML

<!doctype html>
<html ng-app="plunker" >
<head>
  <meta charset="utf-8">
  <title>AngularJS Plunker</title>
  <link rel="stylesheet" href="style.css">
  <script>document.write("<base href=\"" + document.location + "\" />");</script>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.2/angular.js"></script>
  <script src="app.js"></script>
</head>
<body ng-controller="MainCtrl">
  This scope value <input ng-model="name">
  <my-directive ng-model="name"></my-directive>
</body>
</html>

的JavaScript

var app = angular.module('plunker', []);

app.controller('MainCtrl', function($scope) {
  $scope.name = "Felipe";
});

app.directive('myDirective', function($compile) {
  return {
    restrict: 'E',
    scope: {
      ngModel: '='
    },
    template: '<div class="some"><label for="{{id}}">{{label}}</label>' +
      '<input id="{{id}}" ng-model="value"></div>',
    replace: true,
    require: 'ngModel',
    link: function($scope, elem, attr, ctrl) {
      $scope.label = attr.ngModel;
      $scope.id = attr.ngModel;
      console.debug(attr.ngModel);
      console.debug($scope.$parent.$eval(attr.ngModel));
      var textField = $('input', elem).
        attr('ng-model', attr.ngModel).
        val($scope.$parent.$eval(attr.ngModel));

      $compile(textField)($scope.$parent);
    }
  };
});

不过,我没有信心,这是处理这种情况的正确方法,并有我的控制是没有得到与NG-模型目标字段的值进行初始化的错误。

However, I am not confident this is the right way to handle this scenario, and there is a bug that my control is not getting initialized with the value of the ng-model target field.

下面是上面的code的Plunker: http://plnkr.co/edit/IvrDbJ

Here's a Plunker of the code above: http://plnkr.co/edit/IvrDbJ

什么是处理这个正确的方式?

What's the correct way of handling this?

修改:删除 NG-模式=值后从模板,这似乎是工作的罚款。但是,因为我要仔细检查,这是这样做的正确的方式我会继续这个问题打开。

EDIT: After removing the ng-model="value" from the template, this seems to be working fine. However, I will keep this question open because I want to double check this is the right way of doing this.

推荐答案

它实际上是pretty好的逻辑,可以简化事情有点。

It's actually pretty good logic but you can simplify things a bit.

var app = angular.module('plunker', []);

app.controller('MainCtrl', function($scope) {
  $scope.model = { name: 'World' };
  $scope.name = "Felipe";
});

app.directive('myDirective', function($compile) {
  return {
    restrict: 'AE', //attribute or element
    scope: {
      myDirectiveVar: '=',
     //bindAttr: '='
    },
    template: '<div class="some">' +
      '<input ng-model="myDirectiveVar"></div>',
    replace: true,
    //require: 'ngModel',
    link: function($scope, elem, attr, ctrl) {
      console.debug($scope);
      //var textField = $('input', elem).attr('ng-model', 'myDirectiveVar');
      // $compile(textField)($scope.$parent);
    }
  };
});

HTML与指令

<body ng-controller="MainCtrl">
  This scope value <input ng-model="name">
  <my-directive my-directive-var="name"></my-directive>
</body>

CSS

.some {
  border: 1px solid #cacaca;
  padding: 10px;
}

您可以看到它在这个 Plunker 行动。

You can see it in action with this Plunker.

下面是我所看到的:


  • 我明白你为什么要使用吴模型,但在你的情况下,它是没有必要的。 NG-模式是的现有的HTML元素,在该范围内的值联系起来。既然你要创建一个指令自己,你正在创建一个新HTML元素,因此你不需要NG-模式。

  • I understand why you want to use 'ng-model' but in your case it's not necessary. ng-model is to link existing html elements with a value in the scope. Since you're creating a directive yourself you're creating a 'new' html element, so you don't need ng-model.

修改正如他在评论中提及马克,没有任何理由,你的不能的使用NG-模式,只是为了保持与惯例。

EDIT As mentioned by Mark in his comment, there's no reason that you can't use ng-model, just to keep with convention.


  • 将在你的指令明确创建一个范围(一个'隔离'的范围),该指令的范围无法访问名称变父范围(这就是为什么,我想,你想用NG-模型)。

  • 我从你的指令删除ngModel并与您可以更改到任何一个自定义名称替换它。

  • ,使得这一切仍然工作中的事情是'='的范围标志。检出范围标题下的文档文档

  • By explicitly creating a scope in your directive (an 'isolated' scope), the directive's scope cannot access the 'name' variable on the parent scope (which is why, I think, you wanted to use ng-model).
  • I removed ngModel from your directive and replaced it with a custom name that you can change to whatever.
  • The thing that makes it all still work is that '=' sign in the scope. Checkout the docs docs under the 'scope' header.

在一般情况下,你的指令应采用隔离范围(你做了正确的),如果你在你的指令希望值始终映射到父范围值使用'='类型的范围。

In general, your directives should use the isolated scope (which you did correctly) and use the '=' type scope if you want a value in your directive to always map to a value in the parent scope.

这篇关于AngularJS - 创建一个使用NG-model的指令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆