我有两个指令。外部指令需要从服务获取一些信息,然后将该对象用于内部指令。但我无法弄清楚如何正确地做到这一点。这里有一些示例代码,我想我有一些彩色铅笔,我们需要根据ID检索铅笔信息以找出铅笔的颜色:
import time
import datetime
a = (2015, 6, 3, 10, 41, 3)
b = datetime.datetime.strptime(', '.join(str(x) for x in a),'%Y, %m, %d, %H, %M, %S')
print(time.mktime(b.timetuple()))
>>> 1433342463.0
<div pencil-directive pencil-id="7"></div>
我希望看起来像这样:
angular.module('app', [])
.controller('sampleController', function($scope){
$scope.color = "red";
})
.directive('colorDirective', function(){
return {
restrict: "E",
template: "<span ng-class='color'>{{color}}</span>",
scope: {
color: '='
}
}
})
.directive('pencilDirective', ["$timeout", function($timeout){
return{
restrict: "A",
template: "<div>Pencil {{pencilId}} is " +
"<color-directive color='pencil-color'> </div>",
scope: {
pencilId: "@",
pencilColor: "=?"
},
controller: function($scope){
//simulate getting the pencil color from a service
$timeout(function(){
$scope.pencilColor = "blue";
}, 10)
}
}
}])
但它最终只是看起来像这样:
Pencil 7 color is blue.
我认为这是因为Pencil 7 color is 0
在pencilColor
看到colorDirective
之前没有被初始化,并且以后没有更新,但我不知道这种工作的“角度方式”
Here's a plunk.请注意我在Angular 1.2上,因为我们仍然支持IE 8(暂时)。