angularjs-scope

Nested directives - cannot pass args to controller method from child directive in Angularjs

喜欢而已 提交于 2019-12-09 06:04:35
问题 I'm having some trouble with nested directives in angularjs. I want to call a controller method from a directive within another directive and am trying to pass arguments to it, however they are undefined. I'm attempting to call remove() with three arguments from selected.html below. It was working before I introduced a parent directive (televisionFilter.js) Can anyone suggest what to do to pass these to the controller? Thanks! Code: controller.js $scope.remove = function(selectorToRemove,

how to get index position of an array item in controller passing a value in angularjs

泪湿孤枕 提交于 2019-12-09 04:42:45
问题 I have an array with list of values. [Object { id="5", country="UAE"}, Object { id="4", country="India"}] I want to get the index of array item based on the value present in the id. How can I get the index position of an array item with value of id = 4 in angularJS Controller? 回答1: The angularjs way (using $filter) would be something like this app.controller('MainCtrl', ['$scope', '$filter', function($scope, $filter) { //array var items = [{ id: "5", country: "UAE" }, { id: "4", country:

AngularJS changing model value in one controller triggers model update in others

青春壹個敷衍的年華 提交于 2019-12-09 04:27:20
问题 EDIT: Ok I update the example to avoid the loop problem, so back to the original question it sill recalculate B model objects. In this example: http://jsfiddle.net/qn2Wa/ <div ng-app> <div ng-controller="A"><input ng-model="m"> {{a()}} </div> <div ng-controller="B"><input ng-model="m"> {{b()}} </div> </div> JS function A($scope) { $scope.m='a'; var counter = 0; $scope.a = function(){ console.log("A " + counter++); return $scope.m; } } function B($scope) { $scope.m='b'; var counter = 0; $scope

How to pass NgModelController to directive controller?

久未见 提交于 2019-12-09 04:13:24
问题 Can i pass NgModelController to directive controller? That's required so i can assign values to model in controller. This example does not work: angular .module('directives.selectBox', []) .directive('selectBox', selectBox); function selectBox() { return { restrict : 'E', require : 'ngModel', scope : { list : '=', }, replace : true, templateUrl : 'common/directives/selectBox/selectBox.html', controller : SelectBoxController, }; } function SelectBoxController(ngModel) { ngModel.$setViewValue

Angular.js “Controller as …” + $scope.$on

时光怂恿深爱的人放手 提交于 2019-12-09 03:31:53
问题 If i'd like to use the "Controller as ..." syntax in Angular, how should I approach things like $scope.$on(...) that i need to put inside the controller? I get an impression i could do it some other way than the one shown below. Here, to get $scope.$on working i bind "this" to the callback function. I tried to invoke $on on "this" inside the controller but it didn't work. Could you give me a hint here or if i'm completely messing up, could you point me to some right way to do it? Thanks. main

Running $apply on $rootScope vs any other scope

拜拜、爱过 提交于 2019-12-09 02:07:21
问题 The $apply function can run on any scope, including $rootScope . Are there cases when it makes a difference if I run it on my local scope or if I run it on my $rootScope ? I'm asking because I'd like to create a helper function that wraps a given function in an $apply . To do that I'd always need to pass in a scope, which is A) annoying and B) not easy because I don't necessarily have a local scope. I'd like to always have my helper function call $apply on the $rootScope , but not if there's

AngularJS: PUT send data with URL but not as JSON data

[亡魂溺海] 提交于 2019-12-09 01:41:46
问题 Here is my UserService angular.module('userServices', ['ngResource']).factory('User', function($resource) { return $resource('/users/:userId', // todo: default user for now, change it {userId: 'bd675d42-aa9b-11e2-9d27-b88d1205c810'}, {update: {method: 'PUT', params:{profile: '@profile'}, isArray: false}} ); }); In my controller, I do $scope.save = function() { $scope.user.$update({profile: $scope.profile}); } But when I see the Network Tab in Chrome, I see Request URL:http://localhost:5000

unable to access rootscope var in directive scope

我与影子孤独终老i 提交于 2019-12-09 00:29:00
问题 The function below defines a variable in the rootscope. function MyCtrl($scope, $rootScope) { $rootScope.buttons = [{href: '#/students', icon:'icon-ok'}, {href: '#/students', icon:'icon-remove'}, {href: '#/students/new', icon:'icon-plus'}]; } MyCtrl.$inject = ['$scope', '$rootScope']; The html in the directive below depends upon a variable in the rootscope - angular.module('btnbar.directive', []). directive("btnBar", function(){ return { restrict: 'E', scope :{}, controller: function($scope,

angularjs controller instantiation, ui-router

こ雲淡風輕ζ 提交于 2019-12-08 23:13:37
When do controllers get instantiated? Is it the first time you visit that state? also, What happens when you revisit the state, does a new controller get instantiated again? Assume that I have two states, A and B, and I put an alert statement at the top of state B. I noticed that if go from state A to B state B's alert statement sets off which tells me that the controller got instantiated. But suppose I go from state A to B to C and back to B, the alert statement does NOT go off. However, if I go from state A to B to C to B to A to B the alert statement goes off again. Here is a part of my

Angular - broadcast , $on called multiple times in directive

半城伤御伤魂 提交于 2019-12-08 19:47:39
问题 I am working on a single page app, with angular and I have a need to communicate between 2 different directives which basically don't have a parent child relation. In Directive A, I have 2 places where I need to broadcast same event from different functions. And in Directive B, have written a $on listener for that. Now, I observe that the whenever callFirstFunc & its broadcast is called for first time, the listener will be called once. Upon subsequent calling, the listener is called twice,