Angularjs - 属性等于值

时间:2014-01-23 14:23:27

标签: javascript angularjs

有人知道为什么我这样做'drawStock =“drawStock”喜欢

<tr ng-repeat="icl in ic.internal_consumption_lines" drawStock="drawStock(locator_id, product_id)" ...></tr> 

然后在我的剧本中

scope: { 
...
drawStock: "&",
...
},
template:{
...
<span>{{drawStock({locator_id:ic.internal_consumption_lines[index].transaction.locator.id, product_id:ic.internal_consumption_lines[index].transaction.product.id})}}</span>
...
...
$scope.drawStock = (lid, pid) ->
for i of $scope.product_stocks
  if $scope.product_stocks[i].locator_id == lid
    if $scope.product_stocks[i].product_id == pid
      return $scope.product_stocks[i].stock

drawStock函数不会显示库存,但是如果我更改了与该值不同的名称的属性,让我们说类似'drawstock =“drawStock(......'它有效......:

<tr ng-repeat="icl in ic.internal_consumption_lines" drawstock="drawStock(locator_id, product_id)" ...></tr> 

然后在我的剧本中

scope: { 
...
drawstock: "&",
...
},
template:{
...
<span>{{drawstock({locator_id:ic.internal_consumption_lines[index].transaction.locator.id, product_id:ic.internal_consumption_lines[index].transaction.product.id})}}</span>
...
...
$scope.drawStock = (lid, pid) ->
for i of $scope.product_stocks
  if $scope.product_stocks[i].locator_id == lid
    if $scope.product_stocks[i].product_id == pid
      return $scope.product_stocks[i].stock

1 个答案:

答案 0 :(得分:2)

根据 the docs

  

Angular 规范化元素的标记和属性名称,以确定哪些元素与哪些指令匹配。我们通常通过区分大小写的camelCase 规范化名称(例如ngModel)来引用指令。但是,由于HTML不区分大小写,我们通过小写形式引用DOM中的指令,通常在DOM元素上使用dash-delimited属性(例如ng-model)。

     

规范化过程如下:

     
      
  1. 从元素/属性的前面剥离x-data-
  2.   
  3. :-_ - 分隔名称转换为camelCase
  4.   

在您的情况下,当Angular看到drawStock: "&"时,它会将其理解为drawStock: "&drawStock",这会将其误导为在HTML中查找draw-stock
因为HTML中没有draw-stock(但只有drawStock),所以它不起作用!

如果你想在你的HTML中使用camelCase表单(例如因为你发现它更具可读性)并且由于HTML不区分大小写,你应该(1)明确指定属性的名称或(2)在你的HTML中使用小写指示。 E.g:

HTML:<tr ... drawStock="..."> // (this is camelCase)

指令(1):scope: { drawStock: '&drawstock' } // (1st camelCase, 2nd lowercase) 指令(2):scope: { drawstock: '&' } // (this is lowercase)
(上述两个指令均指“camelCased”HTML属性)