2016-07-12 46 views
0

嘿每个人我打电话指令和传递硬编码的指令,但它没有通过。我不能在我的代码中遇到什么问题。 代码:指令属性值不通过

我打电话指令是这样的:

<div ng-controller="EmployeeController"> 
    <off-balance user-key="18" collapse-hide="true"></off-balance> 
</div> 

指令代码:

App.directive('offBalance', ['OffService', 'toaster', OffBalance]); 

function OffBalance(OffService, toaster,) { 
    var directive = { 
      link: link, 
      restrict: 'E', 
      templateUrl: 'app/modules/sometemplate.html', 
      scope: { 
       userKey: '=', 
       collapsehide: '=?', 
      }, 
      scope: true 

     } 
    } 

    return directive; 

    function link(scope, element, attr) { 

     scope.$watch('userKey', function (newVal) { 
      alert("userKey" + newVal); 

     }); 

     scope.$watch('collapsehide', function (newVal) { 
      alert("collapsehide" + newVal); 
     }); 
    } 
} 

用户钥和collapsehide都是不确定的。请大家帮我解决了这个问题

回答

1

删除scope: truecollapseHide: '=?'同上你守望

更换 collapsehide: '=?'
3

请在下面找到工作plunker:

https://plnkr.co/edit/y75jQhDCWlEBb2pDNKEs?p=preview

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

App.directive('offBalance', [OffBalance]); 

function OffBalance() { 
    var directive = { 
    link: link, 
    restrict: 'E', 
    scope: { 
     userKey: '=', 
     collapseHide: '=?' 
    } 
} 
return directive; 
} 


function link(scope, element, attr) { 
    scope.$watch('userKey', function(newVal) { 
    alert("userKey" + newVal); 
}); 
console.log(scope); 
scope.$watch('collapseHide', function(collapseHide) { 
    alert("collapsehide" + collapseHide); 
}); 
} 

下面是是点注意:

  1. scope是您指令中的两次。
  2. 对于属性和指令的命名,应该将collapseh隐藏为角度严格。
  3. return directive应该是内部function OffBalance()
+0

有没有什么办法,我们可以同时使用范围? –

+0

不,你不能。两个范围都有其自己的含义。当我们编写'scope:true'指令时会得到新的作用域,当我们编写'scope:{}'时,指令会获得新的隔离作用域。 – varit05