简体   繁体   English

无法访问指令控制器中的$ scope值

[英]unable to access $scope value in directive's controller

I have a simple app, but some reason I am unable to access the $scope values in the directive's controller. 我有一个简单的应用程序,但有些原因我无法访问指令控制器中的$ scope值。

app.js app.js

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

controller.js controller.js

myApp.controller('controller1',['$scope',function($scope) {
    $scope.helloText = 'text';
}])

directive.js directive.js

myApp.directive('myPanel',function(){
    return {
        restrict : "E",
        scope : {
            someText : "="
        },
        templateUrl : 'someTemplate.html'
        controller : function($scope) {
            console.log($scope.someText);// this is undefined
        }
    };
});

someTemplate.html someTemplate.html

<h1>This is the text {{someText}}</h1>

main.html main.html中

<div ng-controller="controller1">
    <my-panel someText="helloText"></my-Panel>
</div>

Need some pointers to understand what am I doing wrong? 需要一些指示来了解我做错了什么?

Your casing is off. 你的外壳已关闭。

<my-panel some-text="helloText"></my-Panel>

You can read up on the normalization in guide 您可以阅读指南中的规范化

Straight from the source 直接来源

Normalization 正常化

Angular normalizes an element's tag and attribute name to determine which elements match which directives. Angular规范化元素的标记和属性名称,以确定哪些元素与哪些指令匹配。 We typically refer to directives by their case-sensitive camelCase normalized name (eg ngModel). 我们通常通过其区分大小写的camelCase规范化名称(例如ngModel)来引用指令。 However, since HTML is case-insensitive, we refer to directives in the DOM by lower-case forms, typically using dash-delimited attributes on DOM elements (eg ng-model). 但是,由于HTML不区分大小写,我们通过小写形式引用DOM中的指令,通常使用DOM元素上的划线分隔属性(例如ng-model)。

The normalization process is as follows: 规范化过程如下:

Strip x- and data- from the front of the element/attributes. 从元素/属性的前面剥离x-和数据。 Convert the :, -, or _-delimited name to camelCase. 将:, - 或_分隔的名称转换为camelCase。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM