【问题标题】:Unable to manage angular depedencies无法管理角度依赖
【发布时间】:2014-11-20 01:36:37
【问题描述】:

我开始学习 Angular,但我在管理依赖项时遇到了麻烦,并且不明白为什么这不起作用。

我的html:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" ng-app="client">
<head>
    <meta charset="utf-8" />
    <title>Title</title>
    <link rel="stylesheet" type="text/css" href="mainstyle.css">
    <script type="text/javascript" src="includes/files/angular.js"></script>
    <script type="text/javascript" src="includes/files/login/login-directives.js"></script>
    <script type="text/javascript" src="includes/files/logged/logged-directives.js"></script>
    <script type="text/javascript" src="includes/files/application.js"></script>
</head>
<body  ng-controller="ApplicationController as controller"> 

    <logged-content  ng-show="controller.isLogged"></logged-content>

    <login-content  ng-show="!controller.isLogged"></login-content>

</body>
</html>

应用程序.js

(函数() { var app = angular.module('client', ['logged-directives', 'login-directives']);

// Controller which contains informations on the session (connected or not, user informations...)
app.controller('ApplicationController', function($scope){
        $scope.isLogged = false; // Contains true if the user is Logged into the software
        $scope.currentUser = null;
    });

})();

记录的指令:

(function(){
var app = angular.module('logged-directives', []);

app.directive('loggedContent', function(){
            return{
                restrict : 'E',
                templateUrl : 'logged-content.html',
            }
    });

});

登录指令:

(function(){
var app = angular.module('login-directives', []);

app.directive('loginContent', function(){
            return{
                restrict : 'E',
                templateUrl : "includes/files/login/login-content.html",
                controller: function($scope) {

                  this.validateLogin = function(user) {
                    alert("user:"+ user.login + " pass : "+user.password);
                    isLogged = true;
                    currentUser = user;
                  };

                },
                controllerAs: "review"
            }
    });

})();

当我在 chrome 中打开我的 html 文件时,出现以下错误:

错误:[$injector:nomod] 模块 'logged-directives' 不可用! 您要么拼错了模块名称,要么忘记加载它。如果 注册模块确保将依赖项指定为 第二个参数。

(我没有包含登录指令,因为它是相同的,不需要理解这个问题,我对登录指令也有同样的问题)

我错过了什么吗?

感谢您的帮助!

【问题讨论】:

  • 没有名为loggedDirectives的模块,您将其定义为logged-directives。

标签: javascript html angularjs


【解决方案1】:

请更新您创建指令的行:

app.directive('loggedContent', function(){ // L should be lower case

因为,您在 html 中使用了 login-content :)

【讨论】:

  • 谢谢您,我进行了更改,但仍然出现错误。 (我没有粘贴 login-content.html 和 login-directives,因为没有这些文件/指令仍然会出现错误)。也许问题出在我的文件路径上?
  • NP。这很简单。如果您仍然面临错误,请告诉我(最好创建一个小提琴)。
  • 这个错误没关系!我现在希望能够从位于 login-content.html 的表单中更改“ApplicationController”'isLogged' 值,我应该创建一个控制器来检查模板中的用户名/密码,连接到我的服务器,好的,我可以管理它使用 Angular 的文档,但是该控制器如何更改“ApplicationController”的记录值?
  • 由于您没有为 loggedContent 指令创建单独的范围,它将共享它的父范围。请更改如下: app.controller('ApplicationController', function($scope){$scope.isLogged = false;}。现在 isLogged 可以从它的子指令访问。如果您希望一个变量在多个控制器之间共享,那么请改用工厂或服务。
  • 谢谢你,我用你的回答更新了我的第一篇文章。我已经设法获取我的登录表单内容(用户是一个具有登录名和来自表单字段的密码的对象),然后我的想法是将 isLogged 更改为 true 并等待我的索引自行刷新(nb-show ) 属性。我错过了什么吗?我需要刷新索引还是我做错了?
【解决方案2】:

我认为您正在为命名约定而苦苦挣扎 Checkout this URL这会对你有所帮助。

【讨论】:

    【解决方案3】:

    好的,我发现了问题...我忘了在指令文件的末尾添加“()”。

    感谢您的帮助。

    【讨论】:

      猜你喜欢
      • 2021-07-05
      • 1970-01-01
      • 2015-02-02
      • 2015-02-14
      • 1970-01-01
      • 1970-01-01
      • 2019-04-27
      • 1970-01-01
      • 2021-11-18
      相关资源
      最近更新 更多