gpt4 book ai didi

javascript - 在指令外调用作用域变量

转载 作者:行者123 更新时间:2023-11-28 02:58:15 25 4
gpt4 key购买 nike

我正在使用 juqeryUi draggable 来拖动一个 div。我正在存储被拖动到 localStorage 内部的位置,以便在刷新页面时它位于同一位置。我现在要做的是将位置存储在局部变量中并将值分配给另一个 div。

索引.html

    <!DOCTYPE html>
<html lang="en" ng-app="container">
<head>
<meta charset="utf-8">
<title>efoli</title>

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/jquery-ui.min.js"></script>
<script type="text/javascript" src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<script type="text/javascript" src="js/angular.min.js"></script>
<script type="text/javascript" src="containers.js"></script>

<link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/themes/smoothness/jquery-ui.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
<link rel="stylesheet" href="Style/design.css">
</head>
<body ng-controller="ContainerController as contain">

<div id="grid" ng-hide="editing"></div>
<div class="drag resize {{item.class}}" ng-repeat="item in block" id="{{item.id}}" ng-hide="editing" edit>
<h1>{{item.title}}</h1>

<!-- ------- Wont show anything--------- -->
<h2>{{topPosition}}</h2>
<textarea type="text" id="{{item.id}}" style="display:table-cell; width:inherit; height:inherit"></textarea>
</div>
</body>
</html>

容器.js

(function() {
var app = angular.module('container', []);
app.controller('ContainerController', ['$scope', function($scope) {
$scope.block = [
{ "id" : "1",
"class":"fname",
"title" : "First Name",
"text":""},
{ "id" : "2",
"class":"lname",
"title" : "Last Name",
"text" : ""},
{ "id" : "3",
"class":"education",
"title" : "Education",
"text" :""},
{ "id" : "4",
"class":"contact",
"title" : "Contact",
"text" : ""},
{ "id" : "5",
"class":"experience",
"title" : "Experience",
"text" : ""}
];
}]);

app.directive('edit', function(){
return {
restrict:'A',
link: function($scope) {
angular.element(document).ready(function() {
var sPositions = localStorage.positions || "{}",
positions = JSON.parse(sPositions);

$.each(positions, function (id, pos) {
$("#" + id).css(pos)
});

$('.drag').draggable({
grid:[25,25],
scroll: false,
stop: function (event, ui) {
positions[this.id] = ui.position
$scope.topPosition = positions[this.id].top;
$scope.leftPosition = positions[this.id].left;
localStorage.positions = JSON.stringify(positions)
}
}); ...

在 index.html 中,当我调用 topPosition 时,没有填充任何内容。我在 .draggable() 中运行了一个 console.log,我得到了顶部位置的值,但是当我在 .draggable 之外运行 console.log 时,我得到了 undefined。我猜 scope 变量只是局部的,但想知道如何将它设为全局变量,以便我可以在不同的地方使用这些值。

最佳答案

在 Controller 中声明一个作用域变量,

$scope.topPosition = ""; //in the container controller

并通过使用父作用域调用变量,在指令中为该变量分配 topPosition 值,例如

var parentScope = $scope.$parent; // inside the edit directive

在可拖动的内部,

parentScope.topPosition = positions[this.id].top;

这个作用域变量可以用在指令中的其他事件中,也可以用在 Controller 中。

请查找修改后的PLUNKER .希望对您有所帮助。

关于javascript - 在指令外调用作用域变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35854911/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com