角依赖注入,使用服务模块之间传递值 [英] Angular Dependency Injection, Passing values between modules using service

查看:162
本文介绍了角依赖注入,使用服务模块之间传递值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看了一下在角的依赖注入。我想对它进行测试。但正如我注入的模块,我想从一个模块值传递给另外一个(我输在控制台上

I have read about the dependency injection in angular. I thought to test it. But as I inject the module, I want to pass the values from one module to another (I am outputting on console

(编辑:解决它给错误未捕获的错误:没有模块:sisUser angular.min.js:18它甚至不是评估的角度前pression {{2 + 2}})

( Solved it is giving error. Uncaught Error: No module: sisUser angular.min.js:18 It is not even evaluating the angular expression {{2+2}})

让我解释一下情景:

的login.html

普通简单的登录请求两个文本输入的用户名和密码服务的用户。

Plain simple Login for asking for two text input username and password into a service "user".

<html lang="en" ng-app="wbsislogin">
<head>
</head>
<body>
<form>
 <fieldset>
    <label>
      <span class="block input-icon input-icon-right">
         <input type="text" class="span12" ng-model="username" placeholder="Username" />
         <i class="icon-user"></i>
        </span>
    </label>

    <label>
      <span class="block input-icon input-icon-right">
          <input type="password" class="span12" ng-model="password" placeholder="Password" />
          <i class="icon-lock"></i>
      </span>
    </label>

    <div class="space"></div>

    <div class="clearfix">
    <label class="inline">
    <input type="checkbox" class="ace" />
        <span class="lbl"> Remember Me</span>
    </label>

    <button ng-click="loginbtn()" class="width-35 pull-right btn btn-small btn-primary">
       <i class="icon-key"></i>
       Login
    </button>
    </div>

    <div class="space-4"></div>
    </fieldset>
</form>
<script src="angular/angular.min.js"></script>
    <script src="wbsis.js"></script>
</body>
</html>

Dash.html

显示距离的login.html传递的用户名和密码通过注射服务

Showing just the passed username and password from login.html through the injected service

<html lang="en" ng-app="wbsislogin">
<head>
</head>
<body>
<p>{{2+2}}</p>
<div ng-controller="ditest">
    {{ usen }} {{ pasw }}
</div>

<script src="angular/angular.min.js"></script>
    <script src="wbsis.js"></script>
</body>
</html>

wbsis.js

var sisUser = angular.module("wbsislogin",[]);
var wbSis = angular.module("wbsis",["wbsislogin"]); // as per the solution 1

sisUser.controller("loginformCtrl",function($scope,user,$log,$location){
$scope.username = "s";
$scope.password = "test2";
$scope.loginbtn = function(){
    user.usern = $scope.username;
    user.passw = $scope.password;
    $log.log(user.usern,user.passw);
    window.location.href='http://sis_frnt.dev/app/dash.html';

}

});

sisUser.factory("user", [function($log){
var user = {
    usern: '',
    passw: ''
};
return user;
}]);
wbSis.controller("ditest", function($scope, $log, user){
$log.log(user.usern,user.passw);
$scope.usen = user.usern;
$scope.pasw = user.passw;

})

请指导我在哪里做错了。

Please guide where I am doing wrong.

推荐答案

您需要通过模块的名称作为依赖,而不是变量名。

You need to pass the name of the module as a dependency instead of the variable name.

修改

var sisUser = angular.module("wbsislogin",[]);
var wbSis = angular.module("wbsis",["sisUser"]);

要:

var sisUser = angular.module("wbsislogin",[]);
var wbSis = angular.module("wbsis",["wbsislogin"]);

这篇关于角依赖注入,使用服务模块之间传递值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆