2016-03-05 34 views
2

我有一個指令:角1.5指令布爾值不會傳遞到範圍

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

var HelloDirective = function() { 
    return { 
    scope: { 
     list: "=", 
     showValue: "=" 
    }, // use a new isolated scope 
    restrict: 'AE', 
    replace: false, 
    template: '<h3>List value is {{list}}</h3><br/>Raw value is [{{showValue}}] <br/><br/>showValue value is: <span ng-show="showValue">True</span><span ng-hide="showValue">False</span>' 

    }; 
} 

myApp.directive("helloDirective", HelloDirective); 

myApp.controller('MyCtrl', function($scope) { 
    $scope.name = 'Angular Directive'; 
    $scope.osList = "Original value"; 
    $scope.bv = true; 
}) 

這裏是我的HTML:

<div ng-controller="MyCtrl"> 
    Hello, {{name}}! Value is {{bv}} 
    <p list="osList" showValue="bv" class="" hello-directive></p> 
    </div> 

這裏是輸出:

你好,角指示!值爲true

列表值是原值

原始值[]

showValue值是:假

oList顯示正常,但showValue不適當地傳遞布爾,什麼是錯的?看到這個小提琴:

https://jsfiddle.net/mbaranski/guq2qyuc/12/

回答

2

它(屬性)應show-value="bv",而不是showValue="bv"

2

你應該有kebab case(連字符分隔的情況下),屬性名。

這將由指令標準化過程使其成爲camelCase,同時映射到隔離範圍屬性。

showValue="bv" 

應該

show-value="bv" 

Fiddle

+0

角的[文件](https://docs.angularjs.org/guide/directive)使用術語'camelCase'但很好的回答乾脆( _Angular規範化元素的標籤和屬性名稱,以確定哪些元素匹配哪些指令。我們通常通過區分大小寫的camelCase規範化名稱(例如ngModel)來引用指令_) –