我打算在我的大型应用程序中使用AngularJS.所以我正在寻找合适的模块.
是什么区别(角route.js)ngRoute和UI的路由器(角-UI-router.js)模块?
在使用ngRoute的许多文章中,使用$ routeProvider配置路由.但是,当与ui-router一起使用时,路由配置为$ stateProvider和$ urlRouterProvider.
我应该使用哪个模块来提高可管理性和可扩展性?
javascript angularjs angularjs-routing angularjs-module angular-ui-router
我试图用其他常量定义常量,但似乎无法完成,因为当需要的常量取决于它时,初始常量还没有准备好.我想确定这根本不可能.
目前我有这样的常量:
angular.module('mainApp.config', [])
.constant('RESOURCE_USERS_DOMAIN', 'http://127.0.0.1:8008')
.constant('RESOURCE_USERS_API', 'http://127.0.0.1:8008/users')
// Specific routes for API
.constant('API_BASIC_INFORMATION', RESOURCE_USERS_API + '/api/info')
.constant('API_SOCIAL_NETWORKS', RESOURCE_USERS_API + '/api/social')
;
Run Code Online (Sandbox Code Playgroud)
第二个两个常数是我想要完成的
考虑以下jfiddle http://jsfiddle.net/bchapman26/9uUBU/29/
//angular.js example for factory vs service
var app = angular.module('myApp', ['module1', 'module2']);
var service1module = angular.module('module1', []);
service1module.factory('myService', function() {
return {
sayHello: function(text) {
return "Service1 says \"Hello " + text + "\"";
},
sayGoodbye: function(text) {
return "Service1 says \"Goodbye " + text + "\"";
}
};
});
var service2module = angular.module('module2', []);
service2module.factory('myService', function() {
return {
sayHello: function(text) {
return "Service2 says \"Hello " + text + "\"";
},
sayGoodbye: function(text) {
return "Service2 …Run Code Online (Sandbox Code Playgroud) 在Angular 1.2中,ngRoute是一个单独的模块,因此您可以使用其他社区路由器ui.router.
我正在编写一个开源模块,旨在为多种不同的路由器实现工作.那么如何检查哪个路由器已加载或存在?
我正在我的模块中的工厂内执行以下操作,但它不能按照我期望的方式工作:
if (angular.module("ngRoute"))
// Do ngRoute-specific stuff.
else if (angular.module("ui.router"))
// Do ui.router-specific stuff.
Run Code Online (Sandbox Code Playgroud)
对于未加载的模块,它会引发错误.例如,如果应用程序正在使用ui.router,则会为ngRoute检查引发以下错误:
未捕获错误:[$ injector:nomod]模块'ngRoute'不可用!您要么错误拼写了模块名称,要么忘记加载它.如果注册模块,请确保将依赖项指定为第二个参数.
我有两个第三方模块,都定义了一个同名的工厂.显然,如果不诉诸于kludge,我无法控制这些模块的命名.
另外,我还有两个内部模块,每个模块使用两个第三方模块中的不同模块作为依赖项(如下所示).我确信我无法从当前模块的依赖项中未列出的模块访问组件,但事实证明我错了.
在这里,即使own1依赖于thirdParty1(已hello定义为hello world)它在控制器中获取hi there(来自thirdParty2).其他模块的配对也是如此.
有没有办法"隔离"模块,所以我只能使用我明确依赖的东西?如果没有,如果我可以随时达到任何目的,那么拥有模块有什么意义(假设主应用程序模块将其作为依赖项)?此外,如果我有两个模块组件命名hello,我怎么知道将使用哪个?
这是jsbin的http://jsbin.com/vapuye/3/edit?html,js,output
angular.module('app', ['own1', 'own2']);
//third-party modules
angular.module('thirdParty1', []).factory('hello', function () {
return 'hello world';
});
angular.module('thirdParty2', []).factory('hello', function () {
return 'hi there';
});
// "own" modules
angular.module('own1', ['thirdParty1']).controller('Own1Ctrl', function(hello) {
this.greet = hello;
});
angular.module('own2', ['thirdParty2']).controller('Own2Ctrl', function(hello) {
this.greet = hello;
});
Run Code Online (Sandbox Code Playgroud)
结果如下:
<body ng-app="app">
<div ng-controller="Own1Ctrl as own1">
Own1: {{ own1.greet …Run Code Online (Sandbox Code Playgroud) 假设我有两个AngularJS模块,例如foo和bar,它们都定义了一个名为的服务baz.
在我的申请中,我依靠他们说:
var app = angular.module('app', [ 'foo', 'bar' ]);
Run Code Online (Sandbox Code Playgroud)
然后我可以尝试使用baz控制器中的服务
app.controller('blaController', [ '$scope', 'baz', function($scope, baz) {
// ...
}]);
Run Code Online (Sandbox Code Playgroud)
如何定义我想要使用的两种服务中的哪一种?有没有像完全合格的名字?
我花了很长时间来试图覆盖提供给模块配置函数的注入常量.我的代码看起来像
common.constant('I18n', <provided by server, comes up as undefined in tests>);
common.config(['I18n', function(I18n) {
console.log("common I18n " + I18n)
}]);
Run Code Online (Sandbox Code Playgroud)
我们通常的方法是保证I18n在我们的单元测试中被注入了
module(function($provide) {
$provide.constant('I18n', <mocks>);
});
Run Code Online (Sandbox Code Playgroud)
这适用于我的控制器,但似乎配置功能不会查看$provide模块外部的内容.它不是获取模拟值,而是将早期值定义为模块的一部分.(在我们的测试中未定义;在下面的plunker中,'foo'.)
下面是一个工作的plunker(看看控制台); 有谁知道我做错了什么?
我正在使用AngularJS和c#mvc.我有一个主页,用户输入一些数据,应该传递到第二个模块,在那里我使用这些数据进行处理和决策.我必须使用第二个模块中第一个模块中输入或更新的数据.有人可以帮助我如何实现这一目标吗?
我是AngularJS的新手,并通过一些文档和教程来学习.我的问题是参考Egghead的视频系列,特别是这个视频,演示如何组合一个基本的搜索过滤器.我想在一个真正的应用程序中使用它,我正在为一个小蜡烛制造业务的朋友建立但是当我将其修改为她的蜡烛而不是复仇者联盟(如视频中的演示)我得到了这个错误:
未捕获错误:[$ injector:modulerr]由于以下原因无法实例化模块myApp:
错误:[$ injector:nomod]模块'myApp'不可用!您要么错误拼写了模块名称,要么忘记加载它.如果注册模块,请确保指定...
我将视频演示中的一个编辑(在数组中只有3个演员)版本复制到了一个jsfiddle并发现它仍然会产生相同的错误.(作为参考,Egghead演示在这里:http://www.thinkster.io/angularjs/ET1iee6rnm/angularjs-ngfilter).到目前为止,我已经在这个网站上阅读了至少六个类似的问题,并尝试了所提供的每个解决方案,但没有一个摆脱这个错误或导致复仇者搜索 - 在视频演示中工作得很好 - 实际上起作用正常.
HTML:
<div ng-app="myApp">
<div ng-controller="AvengersCtrl">
<input type="text" ng-model="search.$" />
<table>
<tr ng-repeat="actor in avengers.cast | filter:search">
<td>{{actor.name}}</td>
<td>{{actor.character}}</td>
</tr>
</table>
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
使用Javascript:
var myApp = angular.module('myApp', []);
myApp.factory('Avengers', function () {
var Avengers = {};
Avengers.cast = [
{
name: "Robert Downey Jr.",
character: "Tony Stark / Iron Man"
},
{
name: "Chris Evans",
character: "Steve Rogers / Captain America"
},
{
name: "Mark Buffalo", …Run Code Online (Sandbox Code Playgroud) 我有一个模块......
angular.module('myModule', []);
Run Code Online (Sandbox Code Playgroud)
然后是工厂
angular.module('myModule')
.factory('factory1', [
function() {
//some var's and functions
}
]);
Run Code Online (Sandbox Code Playgroud)
然后是另一家工厂
angular.module('myModule')
.factory('factory2', [
function() {
//some var's and functions BUT I want to use some var's from factory1
}
]);
Run Code Online (Sandbox Code Playgroud)
但是我想在factory2中使用factory1中的一些变量,如何将factory1注入factory2?