Javascript 如果指令';s属性等于值

Javascript 如果指令';s属性等于值,javascript,angularjs,Javascript,Angularjs,我有一个在多个地方使用的指令,但是在其中一个指令中,我需要匹配一个值来显示某些元素。以下这些对我来说不适用 <my-directive attr="my.value"></my-directive> 我做错了什么?您使用的表达式检查attr范围是否等于字符串'my.value' 如果您想检查它是否等于my.value的值,您必须这样使用它 <div ng-show="attr == my.value"> Hello world </div> 你

我有一个在多个地方使用的指令,但是在其中一个指令中,我需要匹配一个值来显示某些元素。以下这些对我来说不适用

<my-directive attr="my.value"></my-directive>

我做错了什么?

您使用的表达式检查
attr
范围是否等于
字符串
'my.value'

如果您想检查它是否等于
my.value
的值,您必须这样使用它

<div ng-show="attr == my.value"> Hello world </div>
你好,世界

你必须这样做


var-app=angular.module('miniapp',[]);
app.directive(“myDirective”,function()){
返回{
“限制”:“E”,
“替换”:正确,
“范围”:{
“myValue”:“=attr”
},
“模板”:“你好,世界”
} 
})
app.controller(“myController”,函数($scope){
$scope.my={
“价值”:1
}
})
通过将控制器中的值更改为1和0来测试它

请尝试“@”以绑定指令中的值,它对我有效吗

'use strict';
module.exports = Directive;

function Directive(){
  return {
    restrict: 'E',
    templateUrl: 'directive.html',
    scope: {
      attr: '@'
    }
  }
}

你想要字面上的
“my.value”
?发布你的指令代码。是的-刚刚添加。不清楚你想做什么。您想检查传入指令的内容是否是字面上的
'my.value'
?这很简单,但没有意义。您正在将my.value传递给指令,因此。。。my.value与指令中的scope.attr等效。在指令my.value中未定义您是否在指令的控制器中注入了
$scope
属性?您是否在指令的定义中定义了
attr
作用域:
scope:{“attr”:“=”}
?是的,我也这样做了,
<div ng-show="attr == my.value"> Hello world </div>
<div ng-app="miniapp">
   <div ng-controller="myController">
      <my-directive attr="my.value"></my-directive>
   </div>
</div>


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

app.directive("myDirective", function() {
  return {
        "restrict": "E",
        "replace": true,
        "scope": {
            "myValue": "=attr"
         },
        "template": '<div ng-show="myValue"> Hello world </div>'
    } 
})

app.controller("myController", function($scope) {
   $scope.my = {
      "value": 1
   }
})
'use strict';
module.exports = Directive;

function Directive(){
  return {
    restrict: 'E',
    templateUrl: 'directive.html',
    scope: {
      attr: '@'
    }
  }
}