Tag: angularjs

什么是AngularJS中的状态过滤?

我正在阅读AngularJS开发人员指南,并在“filter”部分( https://docs.angularjs.org/guide/filter#stateful-filters )中看到“状态filter”。 给出这个描述: 强烈build议编写有状态的filter,因为Angular不能优化这些filter的执行,这往往会导致性能问题。 许多有状态的filter可以转换成无状态的filter,只需将隐藏状态作为模型公开,并将其变为filter的参数。 我不熟悉Web开发,所以不知道什么是有状态过滤,Angular文档也没有解释它:(请问有人可以解释一个普通的filter和一个有状态的filter是什么区别?

如何实现自定义指令的ng-change

我有一个像模板的指令 <div> <div ng-repeat="item in items" ng-click="updateModel(item)"> <div> 我的指令被声明为: return { templateUrl: '…', restrict: 'E', require: '^ngModel', scope: { items: '=', ngModel: '=', ngChange: '&' }, link: function postLink(scope, element, attrs) { scope.updateModel = function(item) { scope.ngModel = item; scope.ngChange(); } } } 我想在点击一个项目时调用ng-change ,并且foo的值已经被更改。 也就是说,如果我的指令被实现为: <my-directive items=items ng-model="foo" ng-change="bar(foo)"></my-directive> 我希望在foo的值更新时调用bar 。 使用上面给出的代码, ngChange被成功调用,但是用旧的foo值而不是新的更新值调用。 解决这个问题的一个办法是在超时时间内调用ngChange以便在将来的某个时刻执行它,当foo的值已经被改变时。 […]

更改后的angularJS更新input字段

我正在尝试在Angular中构build一个简单的计算器,如果需要的话,我可以重写这个计算器。 我有这部分工作,但是当我回去在一个或两个字段中input一个数字时,总计不会在现场更新。 这是我的jsfiddle http://jsfiddle.net/YUza7/2/ 表格 <div ng-app> <h2>Calculate</h2> <div ng-controller="TodoCtrl"> <form> <li>Number 1: <input type="text" ng-model="one"> <li>Number 2: <input type="text" ng-model="two"> <li>Total <input type="text" value="{{total()}}"> {{total()}} </form> </div> </div> javascript function TodoCtrl($scope) { $scope.total = function(){ return $scope.one * $scope.two; }; }

AngularJS控制器内部的function

我有代码片段,其中有一个angular模块控制器,但有一个function在同一个控制器内,并与一个调用,这是在我的脑海里提出疑问,这是在Javascript或Angular允许的编码方式? 如果是,那么它是如何读取的? 看下面的代码格式我有: obj.controller('CartController',function($scope){ $scope.totalCart = function(){ var total = 10; return total; } function calculate(){ …Some Logic.. } $scope.$watch($scope.totalCart, calculate); )}; 请帮我理解,这是在Angular / Javascript中允许的控制器中的这种types的函数定义和调用?

AngularJS应用程序中的$ timeout未定义错误

我有以下代码: app.factory('Position', ['$timeout', function() { var position = { latitude: 44, longitude: 26 }; console.log("Timeout started"); $timeout(function() { position.latitude += 15; position.longitude += 15; }, 2000); return position; }]); 我得到$timeout not defined在Javascript控制台中$timeout not defined 。 我没有正确注入服务的依赖关系吗?

警告:试图加载angular度不止一次。 Angular JS

我正在尝试运行Grunt Build后查看我的应用程序。 我使用grunt serve:dist来查看所有生产准备好的版本,但是在浏览器中我得到一个无限循环: 警告:试图加载angular度不止一次。 我已经读过这个,因为TemplateURL:在连接后可能是错误的。 正如在这篇文章中: 试图加载angular度不止一次 但是我如何解决这个问题呢? 这是我的app.js /* global libjsapp:true */ 'use strict'; var libjsapp = angular.module('libjsApp', [ 'ngCookies', 'ngResource', 'ngSanitize', 'ngRoute' ]); libjsapp.config(['$routeProvider', function ($routeProvider, $locationProvider) { $routeProvider .when('/', { templateUrl: 'views/posts.html', controller: 'PostsCtrl' }) .otherwise({ redirectTo: '/' }); }]);

在Angular应用程序的Karmatesting文件中包含依赖项?

我正在尝试开始使用Karmatesting,将它们添加到现有的Angular应用程序中。 这是我的主要应用程序定义文件: angular .module('myApp', [ 'ngRoute', 'moduleAdherence' ]); 这是我的控制器文件: angular .module('moduleAdherence', []) .controller('AdherenceCtrl', ['$scope', function ($scope) { $scope.awesomeThings = [1,2,3,4]; }]); 这是我第一次戳文件: describe('Controller: AdherenceCtrl', function () { beforeEach(module('myApp')); var MainCtrl, scope; beforeEach(inject(function ($controller, $rootScope) { scope = $rootScope.$new(); MainCtrl = $controller('AdherenceCtrl', { $scope: scope }); })); it('should attach a list of awesomeThings to the scope', […]

如何在angularjs中传递参数到$ http?

假设我有两个input框,相应的ng模型为fname和lname。 如果我将http请求称为: $http({method:'GET', url:'/search', params:{fname: fname, lname: lname}}) 它会调用的url: /search?fname=fname&lname=lname 我在后端(python)得到的错误是: cannot concatenate str and nontype objects. 这些参数是否不作为string发送? 如果没有,如何解决这个问题?

如何在AngularJS中调用另一个范围函数

在AngularJS中,我在控制器中有2个示波器function, $scope.fn1 = function(){ //do something A }; $scope.fn2 = function(){ //do something B //I want to call fn1 here. }; 如果我的fn2想打电话给fn1,我该怎么办? 谢谢!

不同的ng-include在同一页上:如何向每个发送不同的variables?

我在我的AngularJS应用程序中有一个页面,我想包含相同的html部分,但具有不同的variables。 如果我在我的主html这样做: <div id="div1" ng-include src="partials/toBeIncluded.html onload="var='A'"> <div id="div2" ng-include src="partials/toBeIncluded.html onload="var='B'"> 和toBeIncluded.html样子 <div>{{var}}</div> 这两个div的样子 <div id="div1"><div>B</div></div> <div id="div2"><div>B</div></div> 我想这与ng-includes中的onload被调用的事实有关。 那么我如何向每个不同的包含发送不同的variables呢?