简体   繁体   English

将范围变量从指令传递给它的控制器

[英]Pass scope variable from directive to it's controller

This is possibly easy, but I have browsed the different questions here on SO and in the Angular documentation and can't really find what I'm looking for. 这可能很容易,但是我在SO和Angular文档中浏览了这里的不同问题,但实际上找不到我想要的东西。

In a directive: 在指令中:

function ssKendoGrid() {
    return {
        scope: {
            dataSource: "="
        },
        template: "<div kendo-grid k-options='gridOptions'></div>",
        controller: "ssKendoGridCtrl",
    }
}

That uses the controller: 使用控制器:

function ssKendoGridCtrl($scope) {
    alert($scope.dataSource);
    //other stuff
}

If I want to access the value of dataSource I assumed I'd be able to do something like this: 如果我想访问dataSource的值,我想我可以做这样的事情:

<div ng-controller="myController">
    <div ss-kendo-grid data-source="test"></div>
</div>

MyController is: MyController是:

function myController($scope) {
    $scope.test = "Tested";
}

But it comes as undefined when I try to alert($scope.dataSource); 但是当我尝试alert($scope.dataSource);它是未定义的alert($scope.dataSource); the value.. 价值..

Now I know I can do this: 现在我知道我可以这样做:

<div ss-kendo-grid="test"></div>

And access it in the directive and controller like this: 并在指令和控制器中像这样访问它:

return {
    scope: {
        ssKendoGrid: "="
    },
    template: "<div kendo-grid k-options='gridOptions'></div>",
    controller: "ssKendoGridCtrl"
}

//In controller
alert($scope.ssKendoGrid);

But I would like to be able to pass in a JSON object to do various things with and this doesn't seem as clean as in the markup I'd like it to be more intuitive to look at the html and know what the dataSource is. 但是我希望能够传递一个JSON对象来执行各种操作,这看起来不像标记中那样干净,我希望它更直观地查看html并知道dataSource是什么。

What I'm really looking for is an understanding of what I'm doing wrong, why doesn't this work?? 我真正要寻找的是对我正在做错的事情的理解,这为什么不起作用? I've obviously not got the right understanding of how to pass various things to the isolated scope of the directive. 我显然对如何将各种内容传递给指令的孤立范围没有正确的认识。

SOLVED 解决了

So, turns out I was using the wrong attribute name. 因此,事实证明我使用了错误的属性名称。 HTML5 recognizes data- as a valid attribute, and Angular ignores the fact that data- is prefixed on the variable, which means that I would need to access the variable this way: HTML5将data-识别为有效属性,而Angular忽略data-前缀在变量上这一事实,这意味着我将需要通过以下方式访问变量:

HTML: HTML:

<div ss-kendo-grid data-source="test"></div>

JS: JS:

return {
    scope: {
        dataSource: "=source"
    },
    template: "<div kendo-grid k-options='gridOptions'></div>",
    controller: "ssKendoGridCtrl"
}

Cheers 干杯

you need to access the directive scope variable as 您需要以以下方式访问指令范围变量

<div ss-kendo-grid data-source="test"></div>

similarly as you name the directive in the HTML markup 与您在HTML标记中命名指令类似

So, turns out I was using the wrong attribute name. 因此,事实证明我使用了错误的属性名称。 HTML5 recognizes data- as a valid attribute, and Angular ignores the fact that data- is prefixed on the variable, which means that I would need to access the variable this way: HTML5将data识别为有效属性,而Angular忽略data前缀在变量上这一事实,这意味着我将需要通过以下方式访问变量:

HTML: HTML:

<div ss-kendo-grid data-source="test"></div>

JS: JS:

return {
    scope: {
        dataSource: "=source"
    },
    template: "<div kendo-grid k-options='gridOptions'></div>",
    controller: "ssKendoGridCtrl"
}

And a better convention is to simply not use a directive with "data-" at the beginning of it. 更好的约定是,不要在指令开头只使用带有“ data-”的指令。

invite.directive('googlePlaces', function (){
  return {
    restrict:'E',
    replace:true,
    // transclude:true,
    scope: {location:'=location'},
    template: '<input id="google_places_ac" name="google_places_ac" type="text" class="input-block-level"/>',
    link: function(scope, elm, attrs){
      var autocomplete = new google.maps.places.Autocomplete($("#google_places_ac")[0], {});
      google.maps.event.addListener(autocomplete, 'place_changed', function() {
        var place = autocomplete.getPlace();
        scope.location = place.geometry.location.lat() + ',' + place.geometry.location.lng();
        console.log(scope.location);
        scope.$apply();
        // scope.$apply(function() {
          // scope.location = location;
        // });
      });
    }
  };
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM