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

      <small id='88D0m'></small><noframes id='88D0m'>

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

      时间:2023-10-11
      • <bdo id='4c9za'></bdo><ul id='4c9za'></ul>

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

                <tbody id='4c9za'></tbody>

                <tfoot id='4c9za'></tfoot>
                <legend id='4c9za'><style id='4c9za'><dir id='4c9za'><q id='4c9za'></q></dir></style></legend>
              • 本文介绍了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 指令中的两种数据绑定方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

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

                相关文章

                最新文章

                1. <legend id='EOn8n'><style id='EOn8n'><dir id='EOn8n'><q id='EOn8n'></q></dir></style></legend>
                    <bdo id='EOn8n'></bdo><ul id='EOn8n'></ul>

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

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

                2. <tfoot id='EOn8n'></tfoot>