javascript - 使用 controllerAs 的指令中的 angularjs Controller 无法工作?

标签 javascript angularjs

我正在尝试将一些数据放入我的指令创建的范围内。这是我的 jsFiddle .

下面的代码运行良好

.directive('directive1', function () {
    return: {
        scope: true,
        controller: function ($scope) {
            $scope.name = 'world';
        }
    }
})

<div directive1>
    <p>{{ name }}</p>
</div>

但是这些代码不起作用

.directive('directive2', function () {
    return: {
        scope: true,
        controller: function () {
            this.name = 'world';
        },
        controllerAs: 'testCtrl'
    }
})

<div directive2>
    <p>{{ testCtrl.name }}</p>
</div>

我的代码有什么问题吗?还是我对 controllerAs 有什么误解?

最佳答案

ControllerAs 对指令的支持 was added in 1.2.0 ,因此您必须使用最新版本,而不是链接 fiddle 中的 1.0.2This way它的工作方式如您所愿。

关于javascript - 使用 controllerAs 的指令中的 angularjs Controller 无法工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18931681/

相关文章:

javascript - 反转大于一定长度的字符串

javascript - 这个 Curl POST 命令的 Angular JS 等价物是什么?

javascript - Angularjs:在另一个https请求后显示远程生成的图像

javascript - jquery 中断循环 onclick 并继续

javascript - Angularjs。 "track by"语句导致undefined不是一个函数

javascript - 将嵌套 json 中的值检索到 ng-repeat 中

javascript - Bootstrap 轮播在最后一项之后停止

javascript - Thor 等效或类似的 Javascript 脚本框架?

javascript - 删除 AngularJs 上刚刚创建的对象

JavaScript 相当于 CSS * (everything) 选择器