AngularJS指令中的双向数据绑定

时间:2012-11-08 17:33:25

标签: javascript data-binding frameworks angularjs

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

在玩几个例子时,我想出了一个*有点*的代码:

HTML

<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();
            }
        }
    }
});

这似乎有效(虽然明显慢于*适当的* angularJS变量绑定)但我认为必须有更好的方法来做到这一点。谁能解释一下这个问题?

3 个答案:

答案 0 :(得分:27)

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

我编辑了您从Angular页面使用的示例并包含了输入。 它对我有用: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而不是将变量绑定到值,如下所示:

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

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

答案 1 :(得分:11)

您的意思是 this

我基本上使用@Flek的例子 唯一的区别是ng-model='title'

进行双向绑定的技巧是ng-model,它在document中说明:

  

ngModel是指示Angular执行双向数据绑定的指令。   它与input,select,textarea一起使用。你可以轻松写作   您自己的指令也可以使用ngModel。

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

答案 2 :(得分:3)

这是一种传递给指令中的回调参数的方法。控制器模板:

    <component-paging-select-directive
            page-item-limit="{{pageItemLimit}}"
            page-change-handler="pageChangeHandler(paramPulledOutOffThinAir)"
            ></component-paging-select-directive>

指令:

angular.module('component')
    .directive('componentPagingSelectDirective', [
        function( ) {
            return {
                restrict: 'E',
                scope: { 
                    // using the '=' for two way doesn't work
                    pageItemLimit:  '@', // the '@' is one way from controller
                    pageChangeHandler: '&'
                },
                controller: function($scope) {   
                    // pass value from this scope to controller method. 
                    // controller method will update the var in controller scope
                    $scope.pageChangeHandler({
                        paramPulledOutOffThinAir: $scope.pageItemLimit
                    })

                }, ...

在控制器中:

angular.module('...').controller(...
        $scope.pageItemLimit = 0; // initial value for controller scoped var

        // complete the data update by setting the var in controller scope 
        // to the one from the directive
        $scope.pageChangeHandler = function(paramPulledOutOffThinAir) {
            $scope.pageItemLimit = paramPulledOutOffThinAir;
        }

注意指令的函数参数(带参数作为键的对象),模板(指令中参数对象的'解包'键)和控制器定义的区别。

相关问题