angularjs-scope

How to call controller function from directive?

Deadly 提交于 2019-12-07 12:28:30
问题 how to call controller function from directive? or howto access directive ng-model from controller? eg. I use angular ui bootstrap time component and when time change I need to notify calling function in controller. I think in general it's typical use case for two way communication between components. appControllers.controller('mainCtrl', ['$scope', function($scope) { $scope.changedTime = function(time) { alert(time); } }]).directive('timePicker',['$http', function($http) { return { restrict:

Best practice in keeping `$watch` functions away from your controllers

主宰稳场 提交于 2019-12-07 11:25:56
问题 I tried to find some good examples on best practice in moving $watch functions from a controller to a factory, for example. What I have found is that actually there isn't a unanimous opinion on what's best to do. I've seen examples of injection the $rootScope into a factory and $watch ing for value changes there. Another suggestion is to avoid them whenever possible, and to use ngChange instead on the element itself, for example: <div ng-model="foo.bar" ng-change="updateValue(foo.bar)"></div>

Destroy scope before change path in ui-router

时间秒杀一切 提交于 2019-12-07 10:50:15
问题 I use UI-Router routing. When I change path from a state to another and going back to same state, I see that old $scope in state is there (with it's properties) . I want to destroy that $scope before state changes, So when I come back to state for second time, there will be a clean new scope. I tried to access scope in this event: $rootScope.$on('$stateChangeStart', function(event, toState, toParams, fromState, fromParams) { // fromState.$scope.$destroy(); }); But there isn't any reference to

Is the angular scope binding &(ampersand) a one time binding?

六月ゝ 毕业季﹏ 提交于 2019-12-07 09:53:41
问题 Is the angular scope binding &(ampersand) a one time binding? I see it referred to as a one-way binding, but is it also one-time? Let's say I have: <my-custom-directive data-item="item" /> And my directive is declared as follows: .directive('myCustomDirective', [ '$log', function ($log) { return { restrict: 'E', templateUrl: '/template.html', scope: { dataItem: '&' } controller: function ($scope) { // .... } }]) The reason I'm asking if the binding is one-time is because that seems to be what

How to make template without ng-repeat and pass data to $scope with angular-drag-and-drop-lists?

为君一笑 提交于 2019-12-07 08:41:03
问题 I want to use angular-drag-and-drop-lists with my own grid template to WYSIWYG editor. How to build my own HTML template without ng-repeat so it will be able to drop items in any predefined column and store information in $scope in which column item has been dropped? I want to store dropped items in columns array: .controller('DragDropGrid', ['$scope', function($scope) { $scope.layouts = { selected: null, templates: [ {name: "Plugin", type: "item", id: 2}, ], columns: [], oldaproachcolumns: [

Compare two fields in angularjs directive

◇◆丶佛笑我妖孽 提交于 2019-12-07 07:33:50
问题 I am trying to create directive which can be used to compare two fields in multiple projects. MarkUp : <div class="form-group"> <input ng-model="user.password" type="password" name="password" /> </div> <div class="form-group"> <input ng-model="user.confpassword" ng-compare="password" name="confpassword" type="password" /> <p ng-show="registrationform.password.$error.ngcompare" class="help-block">Password's don't match</p> Directive : "use strict"; angular.module('app.directive.ngCompare', [])

Calculate average from list of values in JSON

こ雲淡風輕ζ 提交于 2019-12-07 07:01:27
I have a simple ng-repeat that displays a list of values (financial income). How can i calculate the average from this list? <div ng-repeat="data in MyData"> {{ data.income }} </div> <div> Average: </div> Which displays: 11 14 8 9 21 10 2 1 5 13 Thanks thomasnu In HTML Average: {{calculateAverage(MyData)}} Code $scope.calculateAverage = function(MyData){ var sum = 0; for(var i = 0; i < MyData.length; i++){ sum += parseInt(MyData[i], 10); //don't forget to add the base } var avg = sum/MyData.length; return avg; }; You'll have to write a function which takes as argument the integer array and

Use angular.element to get scope object by $ID

。_饼干妹妹 提交于 2019-12-07 06:04:30
问题 I need to pass data from an angular app to scripts that run outside of angular because I do not have access to editing the code of the angular app. Using Angular Batarang and NG-Inspector extensions for Chrome, I can see the JSON object I need to pull from, but I am at a loss of how to start. For instance, in Angular Batarang, the object looks like: $id=5 name: "listing" keys: 0: "alpha" 1: "beta" alpha: user: "test" userID: "12345" beta: address: "555 Elm St" phone: 555.555.5555 My initial

watch ng-model inside directive

喜欢而已 提交于 2019-12-07 04:44:18
问题 I have the following directive: directive('myInput', function() { return { restrict: 'AE', scope: { id: '@', label: '@', type: '@', value: '=' }, templateUrl: 'directives/dc-input.html', link: function(scope, element, attrs) { scope.disabled = attrs.hasOwnProperty('disabled'); scope.required = attrs.hasOwnProperty('required'); scope.pattern = attrs.pattern || '.*'; } }; }); with the following template: <div class="form-group"> <label for="input-{{id}}" class="col-sm-2 control-label">{{label}}

$scope doesn't picks up string from form input - AngularJS, JS

末鹿安然 提交于 2019-12-07 04:04:26
I am trying to implement Change Password feature in MVC (Rest server) application from the User Control Panel but because of some strange reason I can't scope values from form input. My html form: <accordion-group heading="Change password" is-open="changePasswordStatus.open" style="cursor: pointer"> <div> <div> <form class="form-horizontal" role="form"> <form-row model="newPassword" name="New: " size="col-sm-8"></form-row> <form-row model="repeatedNewPassword" name="Repeat: " size="col-sm-8"></form-row> <form-row model="currentPassword" name="Current: " size="col-sm-8"></form-row> <br> </form>