angularjs-scope

AngularJS : How to create a two-way data binding between two isolated controllers and a shared service?

坚强是说给别人听的谎言 提交于 2019-12-18 13:00:49
问题 I am trying to create a two-way data binding between two isolated controllers and a shared service (which provides another isolated scope): app.factory("sharedScope", function($rootScope) { var scope = $rootScope.$new(true); scope.data = "init text from factory"; return scope; }); app.controller("first", function($scope, sharedScope) { $scope.data1 = sharedScope.data; }); app.controller("second", function($scope, sharedScope) { $scope.data2 = sharedScope.data; }); Fiddle: http://jsfiddle.net

Dynamic ng-model binding inside a directive

橙三吉。 提交于 2019-12-18 12:57:06
问题 I'm trying to create a custom component that uses a dynamic ng-model inside-out the directive. As an example, I could invoke different components like: <custom-dir ng-model="domainModel1"></custom-dir> <custom-dir ng-model="domainModel2"></custom-dir> With a directive like: app.directive('customDir', function() { return { restrict: 'EA', require: '^ngModel', scope: { ngModel: '=dirValue', }, template: '<input ng-model="dirValue" />', link: function(scope, element, attrs, ctrl) { scope

What is the default for AngularJS directive scope?

回眸只為那壹抹淺笑 提交于 2019-12-18 12:56:16
问题 What is the default scope value of an AngularJS directive? Of course, it is not isolated scope. It is true or false. I can't find any documentation on what it is. 回答1: "Note, by default, directives do not create new scope -- i.e., the default is scope: false " from Understanding scopes. Using the scope option in directive you can: create a child scope prototypically inherited with scope: true create an isolated scope with scope: {} then you can bind some property to parent scopes with '@', '&

Angular.js How to update the scope from a directive?

梦想与她 提交于 2019-12-18 12:54:21
问题 How can I update scope in directive? <div ng-controller="MyCtrl"> <p t></p> </div> My directive: var myModule = angular.module('myModule', []) .directive('t', function () { return { template: '{{text}}', link: function (scope, element, attrs) { scope.text = '1'; element.click(function() { scope.text = '2'; }); } }; }) .controller('MyCtrl', ['$scope', function ($scope) { }]); After click directive does not update. 回答1: Use $apply method: element.click(function() { scope.$apply(function(){

How to iterate through angular $scope variables with a loop

大城市里の小女人 提交于 2019-12-18 11:47:09
问题 I want to iterate through $scope variables with a for loop like this. In this example the $scope object includes an object accounts inlcuding 5 objects, whose names are numbers from 1 to 5. Each of them has a name. for(var i = 1; i < 5; i++){ $('#name').val($scope.accounts.i.name); } The problem: $scope.accounts.i is undefined because i does not count as a varibale inside the $scope variable. It counts as the letter i, so I see no chance to iterate through a scope with a for loop. When I use

AngularJS: Protecting routes with angularjs depending if the user is authorized?

ぐ巨炮叔叔 提交于 2019-12-18 10:55:32
问题 I have just started out working with an AngularJS app I'm developing, everything is going well but I need a way of protecting routes so that a user wouldn't be allowed to go to that route if not logged in. I understand the importance of protecting on the service side also and I will be taking care of this. I have found a number of ways of protecting the client, one seems to use the following $scope.$watch( function() { return $location.path(); }, function(newValue, oldValue) { if ($scope

How to call a function from another module

家住魔仙堡 提交于 2019-12-18 09:11:40
问题 In my angularJS application, I have two modules : module A and module B. angular.module('A').controller('ACtrl',function($scope){ $scope.alertA = function(){alert('a');} //... }); angular.module('B').controller('BCtrl',function($scope){ //... }); How to call the function alertA in the module B ? 回答1: You need to define a factory in module A : var moduleA= angular.module('A',[]); moduleA.factory('factoryA', function() { return { alertA: function() { alert('a'); } }; }); Then use the alertA

directive not interpolating, in a template string

故事扮演 提交于 2019-12-18 08:45:49
问题 I'm trying to conditionally build a template. I got a k2plugin directive with some divs and spans. According to the pluginui attribute, I want to insert another directive at the end of the template. My code, that follows, interpolates everything but pluginui For example, the last div results in: <div {{pluginui}} width='300' height='420'></div> {{pluginui}} is literal, while it should interpolate to trigger the other directive. Funny thing is, if I put {{pluginui}} elsewhere in the same line

directive not interpolating, in a template string

不问归期 提交于 2019-12-18 08:45:43
问题 I'm trying to conditionally build a template. I got a k2plugin directive with some divs and spans. According to the pluginui attribute, I want to insert another directive at the end of the template. My code, that follows, interpolates everything but pluginui For example, the last div results in: <div {{pluginui}} width='300' height='420'></div> {{pluginui}} is literal, while it should interpolate to trigger the other directive. Funny thing is, if I put {{pluginui}} elsewhere in the same line

How do I generate nested table in one column if it has list of values using angular js?

女生的网名这么多〃 提交于 2019-12-18 07:16:10
问题 index.js var app = angular.module('plunker', []); app.controller('MainCtrl', function($scope) { $scope.name = 'World'; $scope.data =[{"Id":1,"Title":"en-US","Description":"UnitedStates","MyValues":[{"Id":100,"Value":"Save"}]}, {"Id":1,"Title":"en-UK","Description":"UK","MyValues":[{"Id":102,"Value":"Delete"}]}] $scope.cols = Object.keys($scope.data[0]); $scope.notSorted = function(obj){ if (!obj) { return []; } return Object.keys(obj); } }); index.html <table border=1> <thead> <tr> <th ng