javascript - 更改 Angular Directive(指令)中的异步传入数据

标签 javascript angularjs asynchronous angularjs-directive angularjs-scope

我有指令:

angular.module('mymodule').directive('someName', ['$http', function($http) {
    return {
        restrict: 'AE',
        scope: {
            headerId: '=header',
            data: '=data'
        },
        templateUrl: 'path/to/template.html',
        compile: function() {
            return function($scope, $link, $attr) {
                console.log($scope.headerId); // print actual header id
                console.log($scope.data);     // undefined

        /* ... code ... */

重点是 header 被硬编码为指令属性中的数字。一切都很好。但数据是通过 Angular 服务异步从服务器获取的。因此,当编译函数运行时,$scope 中没有数据。我的问题是我想更改数据,以便指令模板可以读取它。

我该怎么做?

最佳答案

向数据添加观察者 范围.$watch('data', function() {...})

关于javascript - 更改 Angular Directive(指令)中的异步传入数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26762948/

相关文章:

javascript - 如何更改脚本以适合下拉列表?

java - 413 请求实体太大 - 使用 Spring Boot 和 Rest 模板

Javascript 异步示例不起作用

javascript - NodeJS 是否为每个请求创建一个新线程?

javascript - 将 axios 响应传递给 React 中的子组件

javascript - Node.JS https 请求返回 JSON

javascript - 在页面加载时显示固定列,当用户单击符号时显示 html 表中的剩余列

javascript - Django 中的 Ajax 请求速度缓慢

javascript - AngularJS 中的代码可见性

javascript - 如何在 angular-bootstrap-datetimepicker 输入字段中显示自定义日期格式?