我正在AngularJS中盯着我.
我创建了一个自定义指令:
app.directive('myScroll',function(){
return {
restrict: 'A',
transclude: true,
template: '<div ng-transclude></div>',
link: function(scope , element , attrs) {
element.addClass('scroll-pane');
scope.$watch('tasks', function(newval, oldval){
if ( newval )
{
console.log(newval);
console.log(newval.length);
}
});
console.log("afer watch exper");
console.log (tasks);
}
};
Run Code Online (Sandbox Code Playgroud)
});
使用以下HTML:
<div my-scroll>
<ul>
<li data-ng-repeat="task in tasks" class="task-wrapper">
<div class="task-element">
<div class="name">{{task.name}}</div>
<div class="text">{{task.action}}</div>
</div>
</li>
</ul>
</div>
Run Code Online (Sandbox Code Playgroud)
任务对象通过控制器调用的服务进行评估(如果需要,我将发布其代码).
在指令代码中,任务对象是未定义的,因为我必须得到任务长度来执行更多的css命令我必须等待ng-repeat完成或者只是等待任务变量将被评估.
由于某种原因,$ watch语句的外部和内部始终未定义任务.我可以在控制台中看到首先打印"看完经验后",然后是"在手表中",但仍然没有值.newval对象有[move2:function]但它的length属性保持返回0,但它保留了一系列资源和我的任务.
我在这里缺少什么以及如何在评估任务变量时执行命令?
谢谢你的帮助.
angularjs angularjs-directive angularjs-service angularjs-scope
角色专家,
我是Angular Js的新手,并尝试使用另一个控制器中的输入更新一个控制器中的输入.我不确定它是否可能?
我创建了js小提琴链接只是为了给出一个想法.
在此示例中,当您在控制器2的输入中输入一个值,然后单击"设置值"时,它会更新biding <li></li>但不会更新控制器1的输入.
提前致谢
我正在构建一个Angular应用程序,它将拥有一个顶级控制器和一个二级控制器.将有n个二级控制器,但我想把全局级函数放在某个地方.我在服务中这样做.
我开始创建一个返回api的单个服务的路径,真的,包含很多功能(下面).该服务返回一个具有两个属性分支的对象,每个属性分支包含一组函数.我如何从另一个中调用其中一个?
globalModule.factory('global', function($http) {
var squares = MyApp.squares; // this is the *only* link from Global namespace to this app
return {
squareMgr: {
getSquaresEarned: function() {
return squares.earned;
},
getSquaresPlaced: function() {
return squares.placed;
},
setThisSquareEarned: function(value) {
squares.earned.push(value);
},
setThisSquarePlaced: function(value) {
squares.placed.push(value);
}
},
missionMgr: {
missionInfo: {},
setMissionInfo: function(missionInfo) {
this.missionInfo = missionInfo
},
complete: function(missionData) {
log('complete called on video at ' + new Date());
missionData.complete = true;
log(angular.toJson(missionData));
$http({
url: '/show/completeMission',
method: "POST", …Run Code Online (Sandbox Code Playgroud) 鉴于我有这样的服务.
angular.module('app')
.factory('Session', function Session($rootScope, $cookieStore) {
var user;
if (user = $cookieStore.get('user')) {
$rootScope.currentUser = user;
}
});
Run Code Online (Sandbox Code Playgroud)
和测试
'use strict';
describe('Service: Session', function () {
var Session,
_rootScope,
_cookieStore;
beforeEach(module('app'));
beforeEach(module(function($provide, $injector) {
_rootScope = $injector.get('$rootScope').$new();
_cookieStore = {
get: angular.noop
};
$provide.value('$rootScope', _rootScope);
$provide.value('$cookieStore', _cookieStore);
}));
beforeEach(inject(function(_Session_) {
Session = _Session_;
}));
it('transfers the cookie under user into the currentUser', function() {
spyOn(_cookieStore, 'get').andReturn('user');
inject(function(_Session_) {
Session = _Session_;
});
expect(_rootScope.currentUser).toEqual('user');
});
});
Run Code Online (Sandbox Code Playgroud)
我最终得到了
Error: [$injector:unpr] Unknown …Run Code Online (Sandbox Code Playgroud) 我有一个问题,了解$ interval返回的承诺如何在Angular中起作用.
比方说,在下面的例子中,我们有一个简单的"API"工厂被称为一个"getStuff",它返回一个项目阵列的方法.我们还有一个控制器在该工厂调用$ timeout:
angular.module("app",[])
.factory('api', function(){
return {
getStuff: function() {
return ["stuff"];
}
};
})
.controller('appCtrl', function($scope, $timeout, api){
$timeout(api.getStuff, 1000)
.then(function(response){
console.log(response);
});
})
Run Code Online (Sandbox Code Playgroud)
这将在1秒后在控制台中记录'["stuff"]',这很好.
所以假设我想通过用$ interval替换$ timeout来每秒调用该方法.现在没有任何反应
angular.module("app",[])
.factory('api', function(){
return {
getStuff: function() {
return ["stuff"];
}
};
})
.controller('appCtrl', function($scope, $interval, api){
$interval(api.getStuff, 1000)
.then(function(response){
console.log(response);
});
})
Run Code Online (Sandbox Code Playgroud)
在这种情况下,$ timeout和$ interval有什么不同?
我感谢所有的帮助!
我将这样的2.1.1版ngtagsinput注入到我的控制器中:
app.controller('homeCtrl', ['$scope','$http','ngTagsInput', function($scope,$http){
}])
Run Code Online (Sandbox Code Playgroud)
*注意:测试!将ngTagsInput添加到函数()中不会有所作为......
我在angularjs lib之后加载了ngtagsinput lib.
我想知道还有什么我可以测试来解决以下错误:
Error: [$injector:unpr] Unknown provider: ngTagsInputProvider <- ngTagsInput
http://errors.angularjs.org/1.2.9/$injector/unpr?p0=ngTagsInputProvider%20%3C-%20ngTagsInput
Run Code Online (Sandbox Code Playgroud)
不要误会我的意思,我知道基本上没有检测到这个提供商.
但我发现了我的lib(没有报告404),我看到它的库中使用了ngTagsInput ....
所以,它真的困扰我同样的解决方案不再适用于这个lib!这个lib有什么特别的,为什么在我遇到所有修复它的标准时会出现这个错误?
我有3个执行类似任务的控制器:
它们每个都是唯一的(尽管它们具有相似的功能)。但是,它们都从定义相同的$scope变量开始:
app.controller("PastController", function ($scope) {
$scope.Outages = "";
$scope.loading = 0;
$scope.nothing = 0;
$scope.error = 0;
//--- code continues ---//
});
app.controller("CurrentController", function ($scope) {
$scope.Outages = "";
$scope.loading = 0;
$scope.nothing = 0;
$scope.error = 0;
//--- code continues ---//
});
app.controller("FutureController", function ($scope) {
$scope.Outages = "";
$scope.loading = 0;
$scope.nothing = 0;
$scope.error = 0;
//--- code continues ---//
});
Run Code Online (Sandbox Code Playgroud)
我可以使用服务或工厂在一个地方初始化这些变量,而不用重复代码吗?
有谁知道AngularJS创建和访问全局常量的最佳方法?
我希望能够执行以下操作:
app.constant('baseURL','http://www.baseurl.com');
Run Code Online (Sandbox Code Playgroud)
并从任何工厂或控制器访问它。
javascript singleton angularjs angularjs-service angularjs-scope
我有两个控制器,其中一个我声明了一个$ scope变量,我希望在第二个控制器中可见.
第一控制器
app.controller('Ctrl1', function ($scope) {
$scope.variable1 = "One";
});
Run Code Online (Sandbox Code Playgroud)
第二控制器
app.controller('Ctrl2', function ($scope, share) {
console.log("shared variable " + share.getVariable());
});
Run Code Online (Sandbox Code Playgroud)
我研究了最好的Angular方法,我发现这是使用服务.所以我添加了一项服务Ctrl1
服务
.service('share', function ($scope) {
return {
getVariable: function () {
return $scope.variable1;
}
};
});
Run Code Online (Sandbox Code Playgroud)
此代码返回此错误:
Unknown provider: $scopeProvider <- $scope <- share
Run Code Online (Sandbox Code Playgroud)
所以我的问题是:可能在控制器之间共享$ scope变量吗?不是最好的角度解决方案或者我错过了什么?
对不起我的琐碎问题,但我是一名Angular初学者.
提前致谢
问候
两个选项,哪个更好,为什么:
例A.
文件1 Users.js::
angular.module('users', []);
Run Code Online (Sandbox Code Playgroud)
文件2 userService.js::
angular.module('users').service('userService', ['$q', UserService]);
function UserService($q) {
var users = [
{
name: 'Bob Smith',
age: 26,
address: 'London',
},
{
name: 'John Simpson',
age: 41,
address: 'New York',
},
{
name: 'Maria West',
age: 36,
address: 'Chicago',
}
];
// Promise-based API
return {
loadAllUsers : function() {
// Simulate async nature of real remote calls
return $q.when(users);
}
};
}
})();
Run Code Online (Sandbox Code Playgroud)
文件3 UserController.js::
angular.module('users').controller('UserController', ['$scope', …Run Code Online (Sandbox Code Playgroud)