javascript - AngularJs 在 Controller 和 Controller 之间共享 $scope

标签 javascript angularjs scope

我不明白为什么我无法从指令访问 $scope 属性,文档说指令不会创建新的作用域,那么为什么我无法访问 $scope 属性?

在 app.js

'use strict';

var climbingApp = angular.module('climbingApp', []);

climbingApp.controller('ctrl', function($scope) {

    $scope.initLocation = {
        lat: -54.798112,
        lng: -68.303375
    };

在 google-map.js

'use strict';

climbingApp.directive('gmap', function() {
        return {
            restrict: 'E',
            replace: true,
            template: '<div id="map-canvas"></div>',
            link: function(scope, iElement, attrs) {
                console.log(scope.initLocation);
            },
        }

    })

位于index.html

  <html ng-app="climbingApp">
     <body>
    <gmap></gmap>

    </body>
    </html>

<script src="//code.angularjs.org/1.2.0-rc.2/angular.min.js"></script>

<script src="app/app.js"></script>
<script src="app/dependencies/google-maps.js"></script>

console.log 始终返回未定义。

最佳答案

您需要将作用域注入(inject) Controller

climbingApp.controller('ctrl', function($scope) {

关于javascript - AngularJs 在 Controller 和 Controller 之间共享 $scope,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18991660/

相关文章:

javascript - 根据条件禁用单击/选择下拉列表中的某些选项

javascript - 来自index.html 文件中的javascript 的JSON

javascript - 用于在最后一个括号 () 之间获取文本的正则表达式

javascript - 使用 node.js 写入二进制数据的问题

javascript - 我如何使用 angularjs 打印星形图案(我可以使用 ng-repeat)

c - 为什么在一个函数中声明的 union 类型在另一个函数中使用是无效的?

javascript - AngularJS 指令不会检测使用 "this"绑定(bind)变量的 Controller 作用域

javascript - js : accessing scope of parent class

javascript - 在 Promise 被拒绝后停止运行进程

javascript - 需要使用箭头键在搜索建议中移动