从指令中的独立作用域获取作用域变量
Get Scope variable from isolated scope in directive
我对Ionic和Angular还是个新手。我使用了一个指令来创建地图,它运行良好。现在,在我的主视图中(附地图(,我有一个提交按钮,用于将指令中的数据/变量和视图中的数据发送到我的主控制器。我一直在网上搜寻,仍然找不出正确的方法。我希望你能帮助我。
指令
angular.module('mase.directives', []).directive('map', ["MapService", function(MapService) {
return {
restrict: 'E',
scope: {
onCreate: '&'
//geoloc: '@'
},
link: function ($scope, $element, $attr) {
function initialize() {
var mapOptions = {
// Set to Philippines
center: new google.maps.LatLng(14.6839606, 121.0622039),
zoom: 16,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
MapService.map = new google.maps.Map($element[0], mapOptions);
MapService.marker = new google.maps.Marker({
map: MapService.map
});
$scope.onCreate({
map: MapService.map, //link map to map in controller
marker: MapService.marker, //link marker to marker in controller
geoloc: MapService.geoloc //link geoloc to geoloc in controller
});
$scope.$parent.longitude = 'sample longitude';
/*
console.log('MapService.map in directive', MapService.map);
console.log('MapService.marker in directive', MapService.marker);
console.log('MapService.geoloc in directive', MapService.geoloc);
*/
// Stop the side bar from dragging when mousedown/tapdown on the map
google.maps.event.addDomListener($element[0], 'mousedown', function (e) {
e.preventDefault();
return false;
});
}
if (document.readyState === "complete") {
initialize();
} else {
google.maps.event.addDomListener(window, 'load', initialize);
}
}
}
}]);
指令控制器
.controller('MapCtrl', ["$scope", "$ionicLoading", "MapService", function($scope, $ionicLoading, MapService) {
$scope.mapCreated = function(map, marker, geoloc) {
$scope.map = map; //sets the map from the directive to the $scope
$scope.marker = marker; //sets the marker from the directive to the $scope
$scope.geoloc = geoloc; //sets the geoloc from the directive to the $scope
console.log('$scope.geoloc in $scope.mapCreated', $scope.geoloc);
$scope.centerOnMe();
};
$scope.centerOnMe = function () {
console.log("Centering");
if (!$scope.geoloc && !$scope.map && !$scope.marker) {
console.log($scope.map);
console.log($scope.marker);
console.log($scope.geoloc);
return;
}
$scope.loading = $ionicLoading.show({
template: 'Getting current location...',
noBackdrop: true
});
MapService.getCurrLoc($ionicLoading);
};
}])
在我的主控制器中
.controller('Submit',function($scope,MapService) {
$scope.doSubmit = function() {
console.log($scope.longitude);
}
})
在我的主html 中
<ion-content>
<form ng-submit="doSubmit()">
<div class="list">
<div class="item">
<span class="input-label">Category <i class="ion-chevron-right"></i> Sub-category</span>
</div>
<ui-view name="incident-map"></ui-view>
<label>LAT: {{longitude}}</label>
<div class="item item-input item-stacked-label">
<span class="input-label">Description</span>
<textarea style="margin-top: 0px; margin-bottom: 0px; height: 95px;"></textarea>
</div>
<div class="item item-button-right">
<a href=""><i class="icon ion-android-camera text-24"></i></a>
<a href=""><i class="icon ion-android-attach text-24"></i></a>
<button class="button button-positive" type="submit">
<span class="text-16">Submit</span>
</button>
</div>
</div>
</form>
</ion-content>
在两个驱动程序中将$rootScope.latitud
的$scope.latitud
更改为"活动"。你也可以制作
<label ng-model="data.latitud">LAT: {{longitude}}</label>
<button class="button button-positive" ng-click="doSubmit()>
<span class="text-16">Submit</span>
</button>
$scope.doSubmit = function(){
alert($scope.data.latitud);
}
相关文章:
- 在put方法之前从作用域获取数据
- 在函数中获取正确的作用域(不使用that=this)
- JavaScript通过名称获取当前作用域中的变量
- 为什么我的作用域函数没有获取从模型传递的数据
- 无作用域指令-如何获取数据
- 获取作用域中变量的第 n 个名称
- 角度 - 从作用域变量中获取 ng 模型名称
- AngularJs 作用域 - 如何正确从 DOM 获取作用域
- 描述从回调方法获取正确的作用域
- 无法在内容脚本Firefox Addon中获取作用域
- 使用this from函数在作用域上获取/设置角度变量
- AngularJS:Can't将变量的值从ctrl作用域获取到指令中
- AngularJS-获取一个输入值并将其传递给父指令作用域
- Angularjs:获取指令中另一个控制器内定义的控制器中作用域变量的值
- 如何在promise解析时手动将promise的结果获取到作用域变量中
- 获取套接字.来自外部作用域的IO作用域
- 动态添加或复制指令不会从父作用域获取变量值
- 从外部作用域获取函数参数计数
- 从指令中的独立作用域获取作用域变量
- 使用隔离作用域获取隐藏按钮的属性