angularjs - Angular bindToController 不起作用,可以将对象从一个 Controller 范围绑定(bind)到另一个

标签 angularjs

我正在使用 Angular 1.5.7 版本。

我有一个指令,它将 Controller 名称和 View 名称作为字符串来分别调用 Controller 。

我无法将用户名绑定(bind)到前一个 Controller 的调用 Controller ,我看到了前一个 Controller 中可用的值。

请你能告诉我可能是什么问题吗?

myApp.directive("pendingRequests", function() {
    return {
        restrict: 'E',
        controller: "@",
        name: "controllerName",
        controllerAs: 'pendingReqCtrl',
        scope: {},
        bindToController: {
            username: '=username'
        },
        templateUrl: function(tElement, tAttrs) {
            return tAttrs.templateUrl;
        }
    };
});

最佳答案

您是否尝试过 $onInit 来初始化您的代码?

angularjs doc says this :

Deprecation warning: although bindings for non-ES6 class controllers are currently bound to this before the controller constructor is called, this use is now deprecated. Please place initialization code that relies upon bindings inside a $onInit method on the controller, instead.



我更新到 1.6.2,我不得不将我的代码放入 $onInit 方法来访问绑定(bind)。

关于angularjs - Angular bindToController 不起作用,可以将对象从一个 Controller 范围绑定(bind)到另一个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42256762/

相关文章:

javascript - AngularJS bootstrap popover 自定义指令

javascript - Angular ng-repeat 值集

angularjs - 以编程方式更改 md-select 中的选定项目

javascript - 将数据从 Angular 发送到在不同域上运行的 Express Nodejs 服务器

javascript - 谷歌地图信息窗口中的 Angular

javascript - 在List onclick事件中添加数据

javascript - Angular ng-route 和 ng-view

javascript - 按 Enter 键不适用于 AngularJS

javascript - 如何使用剑道文件上传异步方法获得响应?

javascript - 如何将 Angular 4 集成到 Rails 应用程序中?Rails 中 Angular 的实际文件夹结构是什么?