简体   繁体   English

处理“ Controller As”语法时,如何访问隔离范围API字段?

[英]How to access Isolated scope API fields when dealing with `Controller As` syntax?

I have a code which allows to select a place from Google maps. 我有一个允许从Google地图中选择地点的代码。

When you select a place , it shows you what you've selected and its LAT/LON : 当您选择一个地点时,它将显示您所选择的内容及其LAT / LON:

在此处输入图片说明

You can see it here 你可以在这里看到

But that code is not using the controller as syntax. 但是该代码controller as语法。

So I've changed it: 所以我改变了它:

  • Controller change : 控制器变更:

在html中:

  • Html change : HTML更改:

在此处输入图片说明

  • Directive change: 指令变更:

在此处输入图片说明

Here is the NEW code 这是新代码

But now when I select a place - it doesn't update the values. 但是现在,当我选择一个地点时-它不会更新值。

Question

How can I fix my code so that it will still interact via : 如何修复我的代码,使其仍然可以通过以下方式进行交互:

scope: {
        details: '=',
        ngAutocomplete: '=',
        options: '=',
        lt:'=',
        ln:'=',
      }

(When I move to controller as ) (当我以controller as移至controller as

I mean - shouldn't I use the controller ( not the scope ) in : 我的意思是-我不应该在以下位置使用controller (而不是scope ):

link: function(scope, element, attrs, controller) ??

What am I missing ? 我想念什么?

If they say that now ( "controller as" syntax ) the controller is our viewModel so I don't see anything wrong with using the controller parameter and access : 如果他们现在说( “ controller as”语法 ),则控制器是我们的viewModel,因此使用controller参数和access不会出现任何问题:

controller.lt = ...;
controller.ln = ...;

And I'm obviously wrong here ... 我显然在这里错了...

You seem to be jumbling up a few concepts because you're trying to use the same controller both in your directive and outside it. 您似乎在弄混一些概念,因为您正在尝试在指令内外使用相同的控制器。

Even if you're using controller As outside your directive, the implementation inside your directive should stay the same. 即使您使用的是controller As在指令内部执行相同的指令。 The whole point is that the directive should not care what's going on outside of it. 重点在于,该指令不应在意它之外的情况。 The scope: {} option allows you to tell the directive which values to put on its own scope, based on the attribute values provided. 使用scope: {}选项,您可以根据提供的属性值告诉指令将哪些值放在自己的范围内。

So the answer is to leave your directive code unchanged from your original example, and everything will work. 因此,答案是使指令代码与原始示例保持不变,并且一切正常。

Working plnkr: http://plnkr.co/edit/xyTX95JA2biYJmWPKxzZ?p=preview 工作的plnkr: http ://plnkr.co/edit/xyTX95JA2biYJmWPKxzZ?p = preview

Side note: You should avoid prefixing your components with ng . 旁注:您应该避免在组件前面加上ng The ng prefix is intended for components that are built into Angular, and you should use your own prefix. ng前缀用于Angular内置的组件,您应该使用自己的前缀。

I ended up using $scope.$watch(); 我最终使用了$ scope。$ watch();。

$scope.$watch(function() {
  return vm.location_result;
}, function(location) {
    if (location) {
       // do something
    }
});

Fiddle Example: http://jsfiddle.net/n3ztwucL/ 小提琴示例: http//jsfiddle.net/n3ztwucL/

GitHub Gist: https://gist.github.com/robrothedev/46e1b2a2470b1f8687ad GitHub Gist: https : //gist.github.com/robrothedev/46e1b2a2470b1f8687ad

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

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