角度组件,控制器未加载

时间:2018-04-29 18:02:16

标签: angularjs

我有以下角度:

angular.module("app.components", []);
angular.module("app", [
    "app.components"
]);

angular.module("app.components")
    .component('testWidget', {
        templateUrl: '/Widgets/TestWidget/Templates/TestWidget.template.html',
        bindings: {
            something: "="
        },
        controller: function () {
            var ctrl = this;
            // ctrl has nothing on it
        }
    });

<div ng-app="app">
    <test-widget something="Shoopy"></test-widget>
</div>

something不是控制器中对象(this)的一部分。我错过了什么?

2 个答案:

答案 0 :(得分:1)

双向绑定(期望父范围属性监视值更改):

bindings: {
    something: "="
}

父控制器需要设置属性:

$scope.Shoopy = "hello world"

对于参数绑定,请使用以下命令:

字符串值绑定:

bindings: {
    something: "@"
}

答案 1 :(得分:0)

您忘了controllerAs,因为您使用的是this而不是$scope

试试这个

angular.module("app.components")
    .component('testWidget', {
        templateUrl: '/Widgets/TestWidget/Templates/TestWidget.template.html',
        bindings: {
            something: "="
        },
        controllerAs: "ctrl",
        controller: function () {
            var ctrl = this;
            // ctrl has nothing on it
        }
    });
相关问题