在AngularjsJS中创建指令

时间:2015-03-27 10:53:17

标签: angularjs angularjs-directive angularjs-scope

范围attr在该指令的每次初始化时都是相同的,为什么?我做错了什么?

我正在创建Expression原型的新实例并将它们添加到items数组中,但它看起来像在post中,attrs对于所有指令都是相同的。

// items in expressions are new constructors

scope.attrs.items = [
 // new Expression, new Expression
];


//ConstructorExpression
function Expression(attrs) {
   this._setProps(attrs);
}

Expression.prototype = {
  _setProps: function (p) {
    p = p || {};

    this.id = isDef(p.id) ? p.id : cgenerator.id;
    this.theme = isDef(p.theme) ? p.theme : 'Expression';
    this.message = isDef(p.message) ? p.message : 'Expression-Change';
  }
};



// Directive
var directive = {
   templateUrl: 'dashboard/widgets/expression/expression.tpl.html',
   controller: ctrl,
   compile: compile,
   scope: {
     attrs: '=?'
   },
   replace: true,
   restrict: 'AC'
 };

 function compile(tElement, tAttrs) {
   return {
     pre: function (scope, jqElm, attr) {
     },
     post: function (scope, jqElm, attr) {
       // scope.attrs is the same in every expression element
     }
   };
 }
 <div class="Expressions_Content">
   <ul>
     <li ng-repeat="expression in attrs.items track by $index">
       <div nt-expression x-attrs="expression"></div>
     </li>
   </ul>
</div>

1 个答案:

答案 0 :(得分:0)

对不起,这不是scope.attrs继承的问题,而是我对我的消息服务的使用。