2014-08-28 113 views
0

我正尝试在angularJs中实现自定义指令的双向绑定。不知何故,它不工作。AngularJS中的自定义指令中的双向绑定

HTML文件

<div ng-app='myApp'>Outside directive 
    <input type='text' ng-model='outAttr'>{{outAttr}}</br> 
    <div my-directive some-attr='outAttr'></div> 
</div> 

JS文件

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

myApp.directive('myDirective', function() { 
    return { 
     restrict: 'A, 
     replace: true, 
     scope: { 
     inAttr: '=someAttr'; 
     }, 
     template: "<div><input type='text' ng-model='inAttr'>\ 
       {{inAttr}}</div>" 
    } 
}) 

不知怎的,它不工作。这里是。有人能帮我指出我的错误吗?谢谢。

回答

1

很少有语法错误。您的代码的逻辑是好的 - jsFiddle

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

myApp.directive('myDirective', function() { 
    return { 
     restrict: 'A', // missing ' 
     replace: true, 
     scope: { 
      inAttr: '=someAttr' // no ; 
     }, 
     template: '<div><input type="text" ng-model="inAttr">{{inAttr}}</div>' // no break 
    }; 
}); 
+0

非常感谢。我觉得 ';'是那里的主要问题。在添加删除时,我无意中将“'”从限制中删除。我的错! 刚才我在[小提琴]上工作(http://jsfiddle.net/chandan_jhun/2naL4bam/7/) – Indyarocks 2014-08-28 18:52:53