我不明白为什么我不能从指令中访问$ scope属性,文档说指令不会创建新的范围,所以为什么我不能访问$ scope属性?
在app.js
'use strict';
var climbingApp = angular.module('climbingApp', []);
climbingApp.controller('ctrl', function($scope) {
$scope.initLocation = {
lat: -54.798112,
lng: -68.303375
};
在谷歌-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
总是返回undefined。
我不知道为什么,因为如果你在回调函数中看到你的控制器,你有没有PARAMS它不为我工作... –
。但是在你的控制器中你使用$ scope。而在AngularJS中,如果你想使用$ scope,你必须将它传递给控制器函数的参数。你到底懂不懂呢 ? –
是的,当然,我已经将$ scope注入到'ctrl'控制器中,并且它不起作用 –