Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在Angular 1.4.5中的ng repeat中使用$parse in指令计算表达式_Javascript_Angularjs_Parsing_Angularjs Ng Repeat_Directive - Fatal编程技术网

Javascript 在Angular 1.4.5中的ng repeat中使用$parse in指令计算表达式

Javascript 在Angular 1.4.5中的ng repeat中使用$parse in指令计算表达式,javascript,angularjs,parsing,angularjs-ng-repeat,directive,Javascript,Angularjs,Parsing,Angularjs Ng Repeat,Directive,我试图在ng repeat中使用指令属性的表达式$parse。 下面是一个简单的HTML: <body ng-app="hello" ng-controller="control"> <div ng-repeat="a in array" my-dir="a.one == 1"></div> </body> 这样做的问题是,$parse正在使用scope查找a.one属性,显然该属性不存在 我可以做my dir=“array[$index].on

我试图在ng repeat中使用指令属性的表达式
$parse
。 下面是一个简单的HTML:

<body ng-app="hello" ng-controller="control">
<div ng-repeat="a in array" my-dir="a.one == 1"></div>
</body>
这样做的问题是,
$parse
正在使用
scope
查找
a.one
属性,显然该属性不存在


我可以做
my dir=“array[$index].one==1”
,但这对指令的用户来说并不直观(他们必须考虑调用指令的范围)。

相反,这里有输入错误-
myDiv

因此,您可以通过require:?ngModel请求父范围。
angular.module('hello', [])
.controller('control', function($scope){
  $scope.array = [{
    one: 1,
    two: 2
  },
  {
    one: 3,
    two: 4
  }];
})
.directive('myDir', function($parse){
  return {
    restrict: 'A',
    link: function(scope, elem, attrs){
      var hash = $parse(attrs.myDiv);
      // I would like a way of evaluating this expression
      // without having to use things like my-dir="array[$index].item == 1"
      // just like one would do with other angular directives.
      console.log(hash(scope)); // This is undefined, because a.one can not be found in scope.
    },
  };

});