<legend id='V2NHd'><style id='V2NHd'><dir id='V2NHd'><q id='V2NHd'></q></dir></style></legend>
      <bdo id='V2NHd'></bdo><ul id='V2NHd'></ul>

  • <small id='V2NHd'></small><noframes id='V2NHd'>

        <i id='V2NHd'><tr id='V2NHd'><dt id='V2NHd'><q id='V2NHd'><span id='V2NHd'><b id='V2NHd'><form id='V2NHd'><ins id='V2NHd'></ins><ul id='V2NHd'></ul><sub id='V2NHd'></sub></form><legend id='V2NHd'></legend><bdo id='V2NHd'><pre id='V2NHd'><center id='V2NHd'></center></pre></bdo></b><th id='V2NHd'></th></span></q></dt></tr></i><div id='V2NHd'><tfoot id='V2NHd'></tfoot><dl id='V2NHd'><fieldset id='V2NHd'></fieldset></dl></div>
        <tfoot id='V2NHd'></tfoot>

        AngularJS 指令中的两种数据绑定方式

        时间:2023-06-14
          <tbody id='xfvM1'></tbody>
            <tfoot id='xfvM1'></tfoot>

            <i id='xfvM1'><tr id='xfvM1'><dt id='xfvM1'><q id='xfvM1'><span id='xfvM1'><b id='xfvM1'><form id='xfvM1'><ins id='xfvM1'></ins><ul id='xfvM1'></ul><sub id='xfvM1'></sub></form><legend id='xfvM1'></legend><bdo id='xfvM1'><pre id='xfvM1'><center id='xfvM1'></center></pre></bdo></b><th id='xfvM1'></th></span></q></dt></tr></i><div id='xfvM1'><tfoot id='xfvM1'></tfoot><dl id='xfvM1'><fieldset id='xfvM1'></fieldset></dl></div>

              <small id='xfvM1'></small><noframes id='xfvM1'>

              • <bdo id='xfvM1'></bdo><ul id='xfvM1'></ul>
              • <legend id='xfvM1'><style id='xfvM1'><dir id='xfvM1'><q id='xfvM1'></q></dir></style></legend>

                1. 本文介绍了AngularJS 指令中的两种数据绑定方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我一直在尝试定义指令,以便可以在表单中显示不同的小部件",具体取决于存储在数据库中的字段类型及其参数.我需要对不同类型的场景做出反应,因此需要指令来处理布局.

                  I have been trying to define directives so I can display different "widgets" in a form, depending on the type of field and its parameters, which are stored in a database. I need to react to different types of scenarios, hence the need for directives to handle layout.

                  在玩一些示例时,我想出了一个 *kinda* 工作的代码:

                  While playing with a few examples, I came up with a code that *kinda* works:

                  <input type="text" ng-model="myModel" style="width: 90%"/>  
                  <div class="zippy" zippy-title="myModel"></div>
                  

                  指令

                  myApp.directive('zippy', function(){
                      return {
                        restrict: 'C',
                        // This HTML will replace the zippy directive.
                        transclude: true,
                        scope: { title:'=zippyTitle' },
                        template: '<input type="text" value="{{title}}"style="width: 90%"/>',
                        // The linking function will add behavior to the template
                        link: function(scope, element, attrs) {
                              // Title element
                              element.bind('blur keyup change', function() {
                                  scope.$apply(read);
                              });
                  
                              var input = element.children();
                  
                  
                              function read() {
                                  scope.title = input.val();
                              }
                          }
                      }
                  });
                  

                  这似乎可行(尽管明显比 *proper* angularJS 变量绑定慢),但我认为必须有更好的方法来做到这一点.谁能解释一下这个问题?

                  This seems to works (albeit noticeably slower than a *proper* angularJS variable binding) but I figure there must be a better way to do this. Can anyone shed some light on the matter?

                  推荐答案

                  我不知道你为什么要手动触发 $apply 方法,因为你实际上并不需要它.

                  I don't know why you are triggering the $apply method manually because you actually don't need it.

                  我编辑了您在 Angular 页面中使用的示例并包含了输入.它对我有用:http://jsfiddle.net/6HcGS/2/

                  I edited the example you used from the Angular page and included the input. It works for me: http://jsfiddle.net/6HcGS/2/

                  HTML

                  <div ng-app="zippyModule">
                    <div ng-controller="Ctrl3">
                      Title: <input ng-model="title">
                      <hr>
                      <div class="zippy" zippy-title="title"></div>
                    </div>
                  </div>
                  

                  JS

                  function Ctrl3($scope) {
                    $scope.title = 'Lorem Ipsum';
                  }
                  
                  angular.module('zippyModule', [])
                    .directive('zippy', function(){
                      return {
                        restrict: 'C',
                        replace: true,
                        transclude: true,
                        scope: { title:'=zippyTitle' },
                        template: '<input type="text" value="{{title}}"style="width: 90%"/>',
                        link: function(scope, element, attrs) {
                          // Your controller
                        }
                      }
                    });
                  

                  更新maxisam 是对的,您必须使用 ng-model 而不是像这样将变量与值绑定:

                  UPDATE maxisam is right, you have to use ng-model instead of binding the variable against the value like so:

                  <input type="text" ng-model="title" style="width: 90%"/>
                  

                  这是工作版本:http://jsfiddle.net/6HcGS/3/

                  这篇关于AngularJS 指令中的两种数据绑定方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:我什么时候应该使用 javascript 框架库? 下一篇:Google 的 Polymer 是一个功能齐全的前端框架,可以替代或补充其他前端框架吗?

                  相关文章

                  <tfoot id='83q9X'></tfoot>

                  <small id='83q9X'></small><noframes id='83q9X'>

                        <bdo id='83q9X'></bdo><ul id='83q9X'></ul>

                    1. <i id='83q9X'><tr id='83q9X'><dt id='83q9X'><q id='83q9X'><span id='83q9X'><b id='83q9X'><form id='83q9X'><ins id='83q9X'></ins><ul id='83q9X'></ul><sub id='83q9X'></sub></form><legend id='83q9X'></legend><bdo id='83q9X'><pre id='83q9X'><center id='83q9X'></center></pre></bdo></b><th id='83q9X'></th></span></q></dt></tr></i><div id='83q9X'><tfoot id='83q9X'></tfoot><dl id='83q9X'><fieldset id='83q9X'></fieldset></dl></div>
                    2. <legend id='83q9X'><style id='83q9X'><dir id='83q9X'><q id='83q9X'></q></dir></style></legend>