angularjs-scope

AngularJS : Directive transcluded scope lost

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-12 09:54:45
问题 I’m building a directive, I’m calling ‘requires-authorization’ to wrap an ng-if directive. I’d like to use it as follows: <requires-authorization role='SuperUser'> <!— super secret user stuff goes here, within the scope of this view's controller —> </requires-authorization> I’ve gotten as far as: angular.module('myApp').directive('requiresAuthorization', function() { return { template: '<div ng-if=\'iAmInRole\' ng-transclude></div>', restrict: 'E', transclude: true, scope: { role: '@' },

how to call function when angular watch cycle or digest cycle is completed

痴心易碎 提交于 2019-12-12 09:32:33
问题 Is there any way to call custom function in angular after angular finishes all watch cycle. Requirement I have multiple watch functions inside my controller. Now I want to execute a function only after all watch functions are executed by angular 回答1: There are couple of ways to do register a callback once a digest is completed. Using $$postDigest : $scope.$$postDigest fires a callback after the current $digest cycle completed. However this runs only once after the next digest cycle. To make

Angular JS : $Scope.Apply()

元气小坏坏 提交于 2019-12-12 09:30:02
问题 I want to know more about $scope.apply() in real time usage. How many times can we use $scope.apply() in a controller? For example, I have some events like ng-click() , ng-change(), ng-blur() etc. All events are in the same controller. For each and every event, should i use $scope.apply()? If yes, I am getting error : Error: [$rootScope:inprog] [http://errors.angularjs.org/1.2.15/$rootScope/inprog?p0=%24apply][1] at Error (native) I have read in this forum that removing the addition $scope

Angular bind service property to a controller scope

不想你离开。 提交于 2019-12-12 09:12:40
问题 I have 2 controllers using the same service, the first controller change the value of a property on the service, I need the second controller to know the property has changed to set the new value in his current $scope. I dont want to use broadcast, is there an elegant way to do this ? Thank you. 回答1: You could create an object in angular service,that will have various shareable properties in it. You could directly assign that variable with your controller scope variable. So that the reference

angularjs directive set template url dynamically

橙三吉。 提交于 2019-12-12 08:26:40
问题 I'm creating a directive with template URL. I want to set the template URL dynamically based on user_role. Any idea? Heres my directive code: RatingRX.directive "headermenu", -> directive = {} directive.restrict = 'E' directive.templateUrl = "../assets/common/headerMenu{{user_role}}.html" directive And I want to set user_role from the controller. Eg: $scope.user_role = 1 回答1: You can pass a function to the templateUrl option and return a string that will be used as a template url at the end.

Can I avoid the object variable name in ng-repeat loop?

早过忘川 提交于 2019-12-12 08:03:25
问题 When defining an ng-repeat directive to iterate over an array, the syntax specifies ng-repeat="friend in friends" , and then within the template you use the interoplation operator like so {{friend.name}} . Is it possible to have the properties assigned to the current item scope, rather than a variable in it? So I could call just {{name}} instead of {{friend.name}} ? The reason being is that my directive is being used in the scope of two different templates - for example I might have a

Angular.js Call $http.get from outside controller

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-12 07:57:46
问题 I have an HTTP resource that returns a JSON list of top 10 entities from a database. I call it this way: var filter= "john"; var myApp = angular.module('myApp', []); myApp.controller('SearchController', ['$scope','$http', function ($scope, $http) { $http.get('/api/Entity/Find/' + filter). //Get entities filtered success(function (data, status, headers, config) { $scope.entities = data; }). error(function () { }); }]); It works! But... how can I change the filter variable in order to change

AngularJS form and null/empty values

眉间皱痕 提交于 2019-12-12 07:57:31
问题 I am working with a somewhat dynamic AngularJS form. In other words, I am able to add rows of input fields, etc. So my approach was to start with a $scope.formData empty object, to encapsulate all the properties that are bound to both static and dynamic HTML form elements. The AngularJS code is as follows: (function() { var formApp = angular.module("formApp", []); formApp.controller("FormCtrl", function ($scope, $timeout) { $scope.formData = {}; $scope.formData.subscribers = [ { name: null,

Share async data between controllers without making multiple requests

不羁岁月 提交于 2019-12-12 07:49:08
问题 I'm trying to make a single $http request to get one of my JSON files and use the data across all my controllers. I saw on egghead.io how to share data across multiple controllers, and I've also read this StackOverflow question: "Sharing a variable between controllers in angular.js". However, the answers there don't use the $http module. When using $http , the controllers don't have the data to work on, and by the time the response is received it's already too late. I then found the method $q

How to use a $watchGroup with object equality or deep-watch the properties in the group?

感情迁移 提交于 2019-12-12 07:06:44
问题 I want a directive to re-render HTML whenever three scope variables have changed. The first two are just integers, the third is an array. We have $watchGroup to watch several variables, we have $watch with objectEquality as a third parameter, and we have $watchCollection which is like $watch , but with objectEquality implied. Is there a way to write a $watch similar to this? $scope.$watchGroup(['number1', 'number2', 'myArray'], callback, true); // true for objectEquality 回答1: Well it seems