javascript - 无法访问指令 Controller 中的 $scope 值

标签 javascript angularjs angularjs-directive angularjs-scope

我有一个简单的应用程序,但由于某些原因我无法访问指令 Controller 中的 $scope 值。

app.js

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

controller.js

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

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

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

ma​​in.html

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

需要一些指导来了解我做错了什么?

最佳答案

您的外壳已关闭。

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

您可以在 guide 中阅读规范化

直接从源头

规范化

Angular normalizes an element's tag and attribute name to determine which elements match which directives. We typically refer to directives by their case-sensitive camelCase normalized name (e.g. 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 (e.g. ng-model).

归一化过程如下:

从元素/属性的前面去掉 x- 和 data-。 将 :、- 或 _ 分隔的名称转换为驼峰命名法。

关于javascript - 无法访问指令 Controller 中的 $scope 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34083207/

相关文章:

javascript - 使用按钮单击禁用 anchor 标记

javascript - JavaScript OOP 中什么时候需要使用 "get"关键字

javascript - 如何使用提交按钮在angularJS中提交表单的所有详细信息

angularjs - 如何从 angular 的 url 中完全删除#hash

angularjs - 具有相同应用程序的多个域运行 firebase 作为后端

解析对象时AngularJS插值错误

javascript - 调用子指令方法的 Angular 指令

javascript - 使用 ReactJS 和 python Flask。一般的做法

javascript - 未捕获的 TypeError : angular. 小写字母不是函数

javascript - ng-repeat 如何处理其中内容的范围变量