指令中的范围不起作用

时间:2015-06-16 11:48:47

标签: html angularjs angularjs-ng-repeat

我想在我的Google地图中添加输入元素。 我写了以下代码:

app.directive('GooglePlaceAutoComplete', function() {
  return {
    restrict:'AEC',
    replace: true,
      scope: {     
        myMap:'='
        },
    template: '<input id="google_places" name="google_places" type="text" class="input-block-level"></input>',
    link: function($scope, elm , attr){

          var input = document.getElementById('google_places');
        var autocomplete = new google.maps.places.Autocomplete(input);
        autocomplete.bindTo('bounds', $scope.myMap);

        google.maps.event.addListener(autocomplete, 'place_changed', function() {
          alert('changed');
        });
    }

  };
});

当我检查元素时,我看到$ scope.myMap ==未定义 因此得到以下错误:

TypeError:无法读取未定义的属性'gm_bindings_'     at kf(https://maps.gstatic.com/maps-api-v3/api/js/21/3/main.js:25:19)     在El.T.(匿名函数).bindTo(https://maps.gstatic.com/maps-api-v3/api/js/21/3/main.js:25:204)     在链接(文件:///Users/ortal/Desktop/Js%20Excersize%202/googlemapjs.js:28:22)     at file:///Users/ortal/Desktop/Js%20Excersize%202/angular.min.js:72:222     at $(file:///Users/ortal/Desktop/Js%20Excersize%202/angular.min.js:72:278)     在N(文件:///Users/ortal/Desktop/Js%20Excersize%202/angular.min.js:61:294)     at g(file:///Users/ortal/Desktop/Js%20Excersize%202/angular.min.js:54:244)     在N(文件:///Users/ortal/Desktop/Js%20Excersize%202/angular.min.js:61:239)     at g(file:///Users/ortal/Desktop/Js%20Excersize%202/angular.min.js:54:244)     在file:///Users/ortal/Desktop/Js%20Excersize%202/angular.min.js:53:282

当我删除以下内容时:

scope: {     
            myMap:'='
            },


<div ng-app="app.ui-map" ng-controller="Controller">
    <section id="map">
        <div id="map_canvas" ui-map="myMap" 
            style="height:300px;width:400px;border:2px solid #777777;margin:3px; border:1px solid" 
            ui-options="mapOptions" 
            ui-event="{'map-idle' : 'onMapIdle()'}"
            >
    </div>

    <div ng-repeat="marker in myMarkers" ui-map-marker="myMarkers[$index]"
                        ui-event="{'map-click': 'markerClicked(marker)'}"></div>
</section>
  <div GooglePlaceAutoComplete></div>

我没有收到任何错误,但地址发生变化时没有发生任何事情

1 个答案:

答案 0 :(得分:0)

尝试添加

def madLibOne():
#Function opens up a file that contains the madlib.
madLib1 = open('MadLibOne.txt', 'r')
madLib1.readline()
#Reads the file and asks user for input in what type of words they want inputted.
weekday = str(input("Enter a weekday: "))
adjective = str(input("Enter an adjective: "))
food = str(input("Enter a food: "))
location = str(input("Enter a location: "))
favoriteprof = str(input("Enter the name of your favorite Proffessor: "))
sumOfMoney = str(input("Enter a sum of money: "))
#Itterates through the file and replaces all words with user input
for line in madLib1:
    line = line.replace("[weekday]",weekday)
    line = line.replace("[adjective]",adjective)
    line = line.replace("[food]",food)
    line = line.replace("[location]",location)
    line = line.replace("[favoriteProf]",favoriteprof)
    line = line.replace("[sumOfMoney]",sumOfMoney)
#closes file
madLib1.close()

指示你的指示。像这样:

scope: true,