[英]Issues with calling function from template
在一种情况下,我无法通过模板在Controller上运行功能。 该值将成为包含函数签名的字符串,而不是应从函数返回的值。
当我在模板标记中使用{{ getSomeObject(d) }}
,它可以正常工作,并打印对象值,这意味着该函数已在Controller上调用。
我尝试过使用{{ }}
和不使用{{ }}
。
伪代码:
<div class"xyz" data-lav-fact="getSomeObject(d)"> <!-- Does not work here -->
{{ getSomeObject(d) }} <!-- Works here -->
</div>
当然,该功能已添加到Controller的作用域中:
$scope.getSomeObject = function(data) {
return { key: "test" };
};
这可以在应用程序的其他部分起作用,在这种情况下,我不知道出了什么问题。 有谁知道这里通常有什么错误?
由于您尝试使用$scope
函数设置属性,因此需要{{ interpolate }}
并使用ngAttr
属性绑定 。 这是一个简单的示例,演示了此操作。 检查注销的元素之间的差异。 进行挖掘时,您会看到{ key: 'test' }
值已设置
<div id="without" data-lav-fact="getSomeObject()">without</div>
<div id="with" ng-attr-data-lav-fact="{{ getSomeObject() }}">with</div>
app.controller('ctrl', ['$scope', function($scope) {
$scope.getSomeObject = function() {
return { key: 'test' };
}
var w = angular.element(document.getElementById('with'));
var wo = angular.element(document.getElementById('without'));
console.log(w[0].attributes); // has value
console.log(wo[0].attributes); // does not have value
}]);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.