[英]how to get a value from my controller in my directive
所以我正在使用angular js,并且我有一个控制器和一个指令。
directive = ->
scope:
items: "=mkTagsInput"
link: (scope, element, attributes, controller) ->
$(element[0]).tagsinput()
$(element[0]).tagsinput('input').typeahead
name: "list"
local: scope.items
好吧,这是我到目前为止的指示(用文字写成)。
<input type="text" data-role="tagsinput" mk-tags-input="labels" />
这就是我调用指令的方式。 问题是我用[]初始化了标签数组。 并且我还提出了Ajax请求来加载数据,但是该指令似乎早已加载,因此在scope.items中始终将[]作为值。
知道我该如何解决吗?
您可以做的是对指令范围items
进行监视。 这样,在更新项目集合时,您会收到通知
scope.$watch('items',function(newValue,oldValue) {
if(newValue) {
//Your item conllection is in newValue
}
});
基本上,任何时候只要引用项目集合发生更改,都会触发手表。 如果您使用的是angularjs 1.2.0 RC,则还要在范围上查找方法watchCollection
。 在此处查看文档http://code.angularjs.org/1.2.0rc1/docs/api/ng。$ rootScope.Scope
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.