Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/434.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 在子指令中获取范围值_Javascript_Angularjs - Fatal编程技术网

Javascript 在子指令中获取范围值

Javascript 在子指令中获取范围值,javascript,angularjs,Javascript,Angularjs,我在用电脑。我在另一个指令中使用这个指令。我的外部指令定义如下: .directive('myDirective', function () { return { restrict:'E', transclude: true, scope: { showLinks: '=?', query: '=' }, templateUrl: '/directives/my-directive.html', controller:

我在用电脑。我在另一个指令中使用这个指令。我的外部指令定义如下:

.directive('myDirective', function () {
  return {
    restrict:'E',
    transclude: true,
    scope: {
      showLinks: '=?',
      query: '='
    },
    templateUrl: '/directives/my-directive.html',
    controller: function ($scope) {
      if (angular.isUndefined($scope.showLinks)) {
        $scope.showLinks = true;
        console.log('show links? ' + $scope.showLinks);
      }
    }
  };
});
<div style="width:100%;">
    <span>{{showLinks}}</span> <!-- renders just fine -->
    <input type="text" autofocus autocomplete="off" class="form-control" ng-model="query"
           typeahead="option as option.Name for option in getLocation($viewValue)"
           typeahead-min-length="3" typeahead-template-url="location.html" />
    <script type="text/ng-template" id="location.html">
      {{showLinks}} <!-- Never renders -->
      <span bind-html-unsafe="match.label | typeaheadHighlight:query"></span>
    </script>
</div>
<my-directive show-links="true" />
my-directive.html如下所示:

.directive('myDirective', function () {
  return {
    restrict:'E',
    transclude: true,
    scope: {
      showLinks: '=?',
      query: '='
    },
    templateUrl: '/directives/my-directive.html',
    controller: function ($scope) {
      if (angular.isUndefined($scope.showLinks)) {
        $scope.showLinks = true;
        console.log('show links? ' + $scope.showLinks);
      }
    }
  };
});
<div style="width:100%;">
    <span>{{showLinks}}</span> <!-- renders just fine -->
    <input type="text" autofocus autocomplete="off" class="form-control" ng-model="query"
           typeahead="option as option.Name for option in getLocation($viewValue)"
           typeahead-min-length="3" typeahead-template-url="location.html" />
    <script type="text/ng-template" id="location.html">
      {{showLinks}} <!-- Never renders -->
      <span bind-html-unsafe="match.label | typeaheadHighlight:query"></span>
    </script>
</div>
<my-directive show-links="true" />

{{showLinks}}
{{showLinks}}
我这样称呼我的指令:

.directive('myDirective', function () {
  return {
    restrict:'E',
    transclude: true,
    scope: {
      showLinks: '=?',
      query: '='
    },
    templateUrl: '/directives/my-directive.html',
    controller: function ($scope) {
      if (angular.isUndefined($scope.showLinks)) {
        $scope.showLinks = true;
        console.log('show links? ' + $scope.showLinks);
      }
    }
  };
});
<div style="width:100%;">
    <span>{{showLinks}}</span> <!-- renders just fine -->
    <input type="text" autofocus autocomplete="off" class="form-control" ng-model="query"
           typeahead="option as option.Name for option in getLocation($viewValue)"
           typeahead-min-length="3" typeahead-template-url="location.html" />
    <script type="text/ng-template" id="location.html">
      {{showLinks}} <!-- Never renders -->
      <span bind-html-unsafe="match.label | typeaheadHighlight:query"></span>
    </script>
</div>
<my-directive show-links="true" />

我需要在我用于typeahead的模板中使用showLinks的值。但由于某些原因,它并没有被传递到模板中。我相信这与范围界定有关。我是通过将值绑定到UI中得出这个结论的,如上所示。showLinks的值在我的模板中显示得很好。但是,它不会出现在我的指令中typeahead实例的模板中

我做错了什么?我觉得我离得很近。然而,我已经为此工作了一整天


谢谢你能提供的任何帮助

typehead指令正在创建自己的隔离作用域,因此您无法访问不属于该作用域的任何属性。基本上,您只能访问以下属性:

scope:{
    index:'=',
    match:'=',
    query:'='
}
但是您可以访问mach
match.model的model属性,因此在您的示例中,您可以将showLinks变量作为参数添加到getLocation方法中,并将其值映射到option对象:

<input type="text" autofocus autocomplete="off" class="form-control" ng-model="query"
           typeahead="option as option.Name for option in getLocation($viewValue, showLinks)"
           typeahead-min-length="3" typeahead-template-url="location.html" />

模板

 <script type="text/ng-template" id="location.html">
      {{match.model.showLinks}} <!-- the show linLinks property is mapped inside getLocation method -->
      <span bind-html-unsafe="match.label | typeaheadHighlight:query"></span>
    </script>

{{match.model.showLinks}
getLocation示例:

$scope.getLocation = function(viewValue, showLinks)
{
   var locations = [];
   // code to fill locations

   // extend each location (option) object with showLinks data
   for (int i=0; i<locations.length; i++)
   {
        var location = locations[i];
        location.showLinks = showLinks;
   }   

   return locations;
$scope.getLocation=function(视图值,显示链接)
{
var位置=[];
//填充位置的代码
//使用showLinks数据扩展每个位置(选项)对象

对于(int i=0;i你的部分的目的是什么?如果你想在showLinks为true时显示span,你可以直接在span标记上放置ng If或ng show。如果getLocation是我控制器上的一个方法,那么showLinks是如何放在match.model上的?我没有在那里建立连接。感谢你的帮助。我想我的困惑来自“我的getLocation方法当前正在返回一个承诺,因为它命中了web服务。@user70192我添加了一个getLocation方法的示例,但它不返回任何承诺。您可以发布getLocation方法吗?”?