我的AngularJS指令遇到了绑定时间问题.它的控制器看起来像这样:
controller: function($element, $scope)
{
$element.find("input").bind("blur", function()
{
SendUpdate();
});
$scope.Subtract = function()
{
Add(-$scope.step);
}
$scope.Add = function()
{
Add($scope.step);
}
function Add(amount)
{
$scope.model = parseInt($scope.model) + parseInt(amount);
console.log("Model: " + $scope.model);
SendUpdate();
}
function SendUpdate()
{
$scope.$emit("ProcessUpdate");
}
}
Run Code Online (Sandbox Code Playgroud)
到目前为止,一切正常,从100开始,加10,打印出Model:110,如预期的那样.但是,当事件由提供模型绑定的变量的父范围处理时,它在事件触发时尚未收到更新的值:
$scope.$on("ProcessUpdate", function()
{
console.log("MyValue: " + $scope.MyValue);
});
Run Code Online (Sandbox Code Playgroud)
打印出MyValue:100,即使它是$ scope.MyValue,它绑定到指令的模型变量(我也使用"="绑定字符).
事实上,该值正在更新.如果我按下一个打印出相同内容的按钮:
console.log("MyValue: " + $scope.MyValue);
Run Code Online (Sandbox Code Playgroud)
它打印出MyValue的正确值:110.所以这显然是一个时间问题.看起来事情按此顺序发生:
我需要的是4在1之后立即发生,因为当事件被触发时我需要父范围是最新的.
我应该采用不同的方式吗?我不想通过事件传递更新的值,因为任何数量的这些指令都可以触发,并且它们需要全部处理父范围.我不想弄清楚改变了什么,并相应地注入它.我只想在父作用域收到指令的新值后触发事件.
谢谢.
我正在从教程EggHead angular.element学习并遵循它来创建测试脚本.但是我的angular.element的行为与教程不同.toggleClass或addClass将"class"添加到"input"和"div".见下面的剧本和图片.这是一个错误吗?或者我错过了什么?
<html ng-app="app">
<head lang="en">
<meta charset="utf-8" />
<title>Debug</title>
<link rel="stylesheet" href="/css/foundation.css" />
</head>
<body>
<dumb-password></dumb-password>
<script src="/js/angular-1.2.0rc1/angular.js"></script>
<script>
angular.module('app', [])
.directive('dumbPassword', function () {
return {
restrict: 'E',
replace: true,
template: '<div><input type="text" ng-model="model.input"><div>{{model.input}}</div></div>',
link: function (scope, element) {
scope.$watch('model.input', function (value) {
if (value === 'password') {
element.children(1).toggleClass('alert-box alert')
} else {
element.children(1).removeClass('alert-box alert')
}
})
}
}
})
</script>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

假设您需要编写一个带有一个attribute-parameter - 控制器事件函数名称的指令.该指令执行一些处理,然后在该事件处理程序上触发通知,并向其传递一些处理参数.
在指令中设置此类属性的建议方法是什么,以避免不必要的开销,如双向绑定?
到目前为止,我只能通过使用双向绑定来实现这一点,如下例所示:
app.controller("testCtrl", function ($scope) {
$scope.onClickEvent = function (ctrlDown) {
alert(ctrlDown);
}
});
app.directive("customInput", function () {
return {
restrict: "E",
scope: {
onClickNotify: "=onClick",
},
template: "<input type='text' ng-click='onClick()' />",
replace: true,
transclude: false,
link: function (scope, element, attrs, controller) {
scope.onClick = function () {
if (typeof (scope.onClickNotify) == 'function') {
scope.onClickNotify(window.event.ctrlKey);
}
}
}
}
});
<custom-input on-click="onClickEvent" />
Run Code Online (Sandbox Code Playgroud)
在上面的例子中,该指令使用"=" - 属性的双向绑定,这是我能够使它工作的唯一方法.我无法理解的是为什么我们不能使用"&"?根据AngularJS文档,我们应该能够在传递带名称的参数时,例如:
scope.onClickNotify({ctrlDown: window.event.ctrlKey});
Run Code Online (Sandbox Code Playgroud)
但它只是不起作用.如果我尝试在事件名称中指定参数,如下所示:
<custom-input on-click="onClickEvent(ctrlDown)" />
Run Code Online (Sandbox Code Playgroud)
然后它仍然无法正常工作.相反,我在指令中传递的值将被忽略.
我对背景中真正发生的事情感到困惑,为什么不按预期使用"&"?在这种情况下,双向绑定看起来像是一个开销,因为我们只是在一个方向传递函数名称,如果不是作为一个简单属性(使用"@").
如果我在这里做错了什么,那么正确的方法是什么?
我一直在使用mysql很长一段时间,但没有真正研究它的一个功能 - 转储中的指令.我目前正在进行分区,所以让我们以一个例子为例,让我们看一下http://dev.mysql.com/tech-resources/articles/mysql_55_partitioning.html中的代码:
show create table t2\G
*************************** 1. row ***************************
Table: t2
Create Table: CREATE TABLE `t2` (
`dt` datetime DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1
/*!50500 PARTITION BY RANGE (to_seconds(dt))
(PARTITION p01 VALUES LESS THAN (63426787200) ENGINE = MyISAM,
PARTITION p02 VALUES LESS THAN (63426816000) ENGINE = MyISAM,
PARTITION p03 VALUES LESS THAN (63426844800) ENGINE = MyISAM,
PARTITION p04 VALUES LESS THAN (63426873600) ENGINE = MyISAM,
PARTITION p05 VALUES LESS THAN (63426902400) ENGINE = MyISAM,
PARTITION …Run Code Online (Sandbox Code Playgroud) 我正在编写一个基于d3.js的实时图表指令.结构如下所示:
myDirective.js:
app.directive('myDirective', function(socketio) {
return {
restrict: 'EA',
templateUrl: '../../views/partials/chart.html',
controller: DataController,
link: function(scope, elem, attrs, ctrl) {
scope.Watch = scope.$watch(function() {
return ctrl.data;
}, function(newVal) {
// d3 code part
});
socketio.on(event, function(newdata) {
ctrl.data.push(newdata);
// redraw chart
});
}
};
});
Run Code Online (Sandbox Code Playgroud)
在上面的d3代码部分中,我指的是http://bl.ocks.org/gniemetz/4618602.主要代码几乎相同,图表显示良好.
现在我想使用socket.io通过代码更新图表
socketio.on(event, function(newdata) {
ctrl.data.push(newdata);
// redraw chart
});
Run Code Online (Sandbox Code Playgroud)
但不知道如何使用更新的'ctrl.data'有效地重绘图表.我知道在Morris.js中,我们可以通过'.setData(ctrl.data)'方法完成此操作,但不知道如何在d3中更新.任何的想法?
ps:我试图将上面的d3代码复制/粘贴到这个地方,但总有一个错误说:"TypeError:t.slice不是函数"
谢谢,
我正在尝试使用ng-repeat和指令显示数组的元素.指令部分对解决方案很重要.但是,数组的元素未绑定并显示空值.
小提琴可以在http://jsfiddle.net/qrdk9sp5/找到
HTML
<div ng-app="app" ng-controller="testCtrl">
{{chat.words}}
<test ng-repeat="word in chat.words"></test>
</div>
Run Code Online (Sandbox Code Playgroud)
JS
var app = angular.module('app', []);
app.controller("testCtrl", function($scope) {
$scope.chat = {
words: [
'Anencephalous', 'Borborygm', 'Collywobbles'
]
};
});
app.directive('test', function() {
return {
restrict: 'EA',
scope: {
word: '='
},
template: "<li>{{word}}</li>",
replace: true,
link: function(scope, elm, attrs) {}
}
});
Run Code Online (Sandbox Code Playgroud)
OUTPUT
["Anencephalous","Borborygm","Collywobbles"]
•
•
•
Run Code Online (Sandbox Code Playgroud)
预期产出
["Anencephalous","Borborygm","Collywobbles"]
•Anencephalous
•Borborygm
•Collywobbles
Run Code Online (Sandbox Code Playgroud)
感谢您的帮助
我需要为“位置指令”应用一些配置。
这些配置必须应用于所有位置,除了某些位置URIs(例如,我不想更改的配置/products,因此,波纹管配置必须应用于除/ products之外的所有位置)
<Location />
# desired configurations
</Location>
Run Code Online (Sandbox Code Playgroud) 我想知道是否可以将参数发送到#define宏以选择不同的输出
例如:
#define Row(1) LPC_GPIO0
#define Row(2) LPC_GPIO3
#define Row(3) LPC_GPIO2
Run Code Online (Sandbox Code Playgroud)
然后在我的代码中,我创建了一个发送参数的循环
Row(x)
Run Code Online (Sandbox Code Playgroud) 我尝试将#error指令与GCC编译器一起使用,如下所示:
#error "The charging pins aren't differing! One pin cannot be used for multiple purposes!"
Run Code Online (Sandbox Code Playgroud)
这说,我应该使用双引号,所以参数将是一个单独的字符串常量,我可以在其中使用撇号.但是,我希望这个字符串出现在多行代码的源代码中,如:
#error "The charging pins aren't differing!
One pin cannot be used for multiple purposes!"
Run Code Online (Sandbox Code Playgroud)
然后,我收到一些错误消息:
warning: missing terminating " character
#error "The charging pins aren't differing! One pin
error: missing terminating " character
cannot be used for multiple purposes!"
Run Code Online (Sandbox Code Playgroud)
如果我在第一行的末尾插入一条黑色长片,则诊断消息将包含第二行开头和第一个单词(One)之间的所有空格.如果两行都是字符串,则诊断消息显示内部双引号.
所以问题是:我如何实现这一输出?(或类似的双引号,但包括撇号)
#error "The charging pins aren't differing! One pin cannot be used for multiple purposes!"
Run Code Online (Sandbox Code Playgroud)