首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从指令到对象的角度赋值

从指令到对象的角度赋值
EN

Stack Overflow用户
提问于 2015-09-17 21:44:43
回答 1查看 292关注 0票数 0

我有这个自定义指令:

代码语言:javascript
复制
var geo = angular.module('Geo', ['Gealocation']);

function SearchForm($scope){
$scope.location = '';
$scope.doSearch = function(){
    if($scope.location === ''){
        alert('Directive did not update the location property in parent    controller.');
    } else {
        alert('Yay. Location: ' + $scope.location);
    }
  };
}
/* Directives */
angular.module('Gealocation', []).
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="form-control"/>',
        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();
            });
        }
    }
});

geo.controller('SearchForm', SearchForm);

在index.html中,我只有很少的输入和自定义指令:

代码语言:javascript
复制
<input type="text" class="form-control" ng-model="meeting.topic"></input>
<input type="text" class="form-control" ng-model="meeting.when"></input>
<input type="text" class="form-control" ng-model="meeting.level"></input>
<input type="text" class="form-control" ng-model="meeting.describe"></input>

<google-places location=location></google-places>
<button ng-click="doSearch()" class="btn btn-large btn-primary">Search!</button>

要显示来自指令的值(带有lat和lng的位置),我可以这样做:

代码语言:javascript
复制
{{location}}

但是我如何才能将这个位置分配给这样的东西:

代码语言:javascript
复制
meeting.location

因为我需要通过稍后的对象会议

EN

回答 1

Stack Overflow用户

发布于 2015-09-18 18:35:24

我通过以下方式解决了这个问题:

在控制器中:

代码语言:javascript
复制
$scope.meeting = {
location: ''
};

视图中:

代码语言:javascript
复制
{{meeting.location = location}}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32631899

复制
相关文章

相似问题

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