首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >$scope内部指令不起作用

$scope内部指令不起作用
EN

Stack Overflow用户
提问于 2016-01-13 13:34:53
回答 1查看 336关注 0票数 0

我跟随This fiddle在我的ionic应用程序中实现了同样的功能。

以下是我的指令:

代码语言:javascript
复制
angular.module('mobApp.services').
            directive('googlePlaces', function(){
                return {
                    restrict:'E',
                    replace:true,
                    // transclude:true,
                    scope: {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();
                            $scope.$apply();
                        });
                    }
                }
            });

下面我在控制器中使用它:

代码语言:javascript
复制
angular.module("mobApp.controllers")
.controller("AutocompleteTextboxController",function($scope) {

   $scope.location = '';
  $scope.selectionDone = function() {
      alert('Yay. Location: ' + $scope.location);
  }
});

在这里,我将这个指令称为

代码语言:javascript
复制
<ion-view view-title="{{title}}">
<ion-nav-bar class="bar-positive">          
    <ion-nav-back-button></ion-nav-back-button>
    <ion-nav-buttons side="right">
      <button class="button button-icon icon ion-android-done" ng-disabled="selectedItem.id==''" ng-click="selectionDone()"></button>
    </ion-nav-buttons>    
</ion-nav-bar>
  <ion-content>
    <div class="list">
      <ion-list ng-show="selectionType == 'place'">
        <google-places location=location></google-places>
      </ion-list>
    </div>    
  </ion-content>
</ion-view>
EN

回答 1

Stack Overflow用户

发布于 2016-01-13 14:10:25

尝试替换:

代码语言:javascript
复制
$scope

使用

代码语言:javascript
复制
scope

在你的指令中。$表示您正在注入作用域,而实际上您并不在链接函数中。对此的一个很好的解释是可用的here

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34759096

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档