我打电话HTML页面内从内script标签控制器的功能如下数据绑定不工作
<script>
function initMap() {
var scope = $('#loginDetailPage').scope();
scope.showLocationDetail();
console.log('scope', scope);
}
</script>
<div id="loginDetailPage" class="canvas">
<div class="container-fluid text-center">
<div class="row content">
<div class="grid profile">
<div class="col-md-8" style="text-align:right;" >
<p><a href="" title="Change background"><i class="fa fa-pencil-square-o"> Add photos to gallery</i></a></p>
<div class="grid-header-business">
</div>
</div>
<div class="col-md-4" style="text-align:left; color:black;">
<h3>{{vm.locationDetailObj.name}}</h3>{{vm.data.test}}
<hr>
<label> Address</label>
<p>{{vm.locationDetailObj.formatted_address}}</p>
<p>Phone: {{vm.locationDetailObj.formatted_phone_number}}</p>
<small rating-stars rating="vm.locationDetailObj.rating"></small>
</div>
</div>
</div>
</div>
<div class="grid-body">
<div class="tab-content">
<div class="tab-pane active" id="timeline">
<p class="lead">Location</p>
<hr>
<div class="row">
<div id="map" style="width:70%;height:400px;"></div>
<script src="https://maps.googleapis.com/maps/api/js?key=API_KEY=places&callback=initMap"></script>
</div>
</div>
</div>
</div>
</div>
这里是我的控制器功能
(function() {
angular.module('myApp').controller('locationDetailCtrl', ['$scope', 'location', '$routeParams', function ($scope, location, $routeParams){
var vm = this;
$scope.showLocationDetail = function() {
vm.locationObj = location.getLocationDetail();
var map = new google.maps.Map(document.getElementById('map'), {
center: {lat: vm.locationObj.geometry.location.lat, lng: vm.locationObj.geometry.location.lng},
zoom: 15
});
var infowindow = new google.maps.InfoWindow();
var service = new google.maps.places.PlacesService(map);
service.getDetails({
placeId: vm.locationObj.place_id
}, function(place, status) {
vm.locationDetailObj = place;
console.log('place: ', vm.locationDetailObj);
if (status === google.maps.places.PlacesServiceStatus.OK) {
var marker = new google.maps.Marker({
map: map,
position: place.geometry.location
});
google.maps.event.addListener(marker, 'click', function() {
infowindow.setContent('<div><strong>' + place.name + '</strong><br>' +
'Place ID: ' + place.place_id + '<br>' +
place.formatted_address + '</div>');
infowindow.open(map, this);
});
}
});
}
}]);
})();
这里数据绑定不适用于$ scope.showLocationDetail函数中的变量。但是,如果我在showLocationDetail函数之外的$ scope对象上写入任何内容,数据绑定将按预期工作。
PS:我从HTML页面中的脚本标记调用$ scope.showLocationDetail。
你的html的'ng-controller'指令在哪里? – JustinJmnz
@JustinJmnz我没有写完整的代码。我正在使用ng-route。它获取模板并初始化控制器。我在控制器的测试功能之外写了任何东西,它正在按预期工作 – ot954
什么是'数据'?您无法将属性添加到未定义的内容。 – JustinJmnz