angularjs-controller

What are the differences in angular controller notation?

↘锁芯ラ 提交于 2019-12-02 03:04:36
问题 I've have seen a couple of notations to initialize a controller in angular, those are: app.controller('nameCtrl', function($scope, ... ){}) and app.controller('nameCtrl', ['$scope','...',function($scope,...){}]) Both work, but I couldn't find anything in the documentation that spot the differences, does it even matter? 回答1: app.controller('nameCtrl', function($scope, ... ){}) The above won't work with minification, but the below will. app.controller('nameCtrl', ['$scope','...',function($scope

What are the differences in angular controller notation?

孤者浪人 提交于 2019-12-02 01:23:41
I've have seen a couple of notations to initialize a controller in angular, those are: app.controller('nameCtrl', function($scope, ... ){}) and app.controller('nameCtrl', ['$scope','...',function($scope,...){}]) Both work, but I couldn't find anything in the documentation that spot the differences, does it even matter? Ajay Beniwal app.controller('nameCtrl', function($scope, ... ){}) The above won't work with minification, but the below will. app.controller('nameCtrl', ['$scope','...',function($scope,...){}]) 来源: https://stackoverflow.com/questions/17977082/what-are-the-differences-in-angular

Difference in controller declaration in AngularJS

感情迁移 提交于 2019-12-02 00:06:29
I have seen controller being declared in two ways as below. But what diff does this make? appmodule.controller('Actrl',['$scope',function($scope) {}]); appmodule.controller('Actrl',function($scope) {}); But, most of the times, the 1st doesn't work. Why? Both the syntax are same but the first one is preferred (there is a typo, see below description) if you are minifying your code. Angular resolves the dependency based on the name so when you write appmodule.controller('Actrl',function($scope) {}); syntax, Angular injects the dependency of $scope by reading the argument name $scope . But when

Angular communication between controllers and directives

你说的曾经没有我的故事 提交于 2019-12-02 00:05:57
I have this piece of code which allows a user to leave comments on a list of items. I created a directive and listen to keydown in order to let the user submit a comment if keyCode == 13 . Not sure if I should include the code to post a comment within the directive. What is the best way to communicate between controllers and directives? I also check whether or not the input is empty before submitting the comment. It works but not sure this is Angular best practice? Here is my plunker . You generally don't want your directives knowing anything about your controller, so the best(Angular) way of

Angular communication between controllers and directives

断了今生、忘了曾经 提交于 2019-12-02 00:02:37
问题 I have this piece of code which allows a user to leave comments on a list of items. I created a directive and listen to keydown in order to let the user submit a comment if keyCode == 13 . Not sure if I should include the code to post a comment within the directive. What is the best way to communicate between controllers and directives? I also check whether or not the input is empty before submitting the comment. It works but not sure this is Angular best practice? Here is my plunker. 回答1:

Communicating between controllers in AngularJs

落花浮王杯 提交于 2019-12-01 23:53:52
I have a simple question: what's the best ('cleanest', 'scaleable') path one should go when it comes to interact between (let's say) two controllers. Would that be to define a service and watch that service's return-value in order to react? I setup a simple example here , where I watch the service's current value: $scope.$watch( function() { return myService.getValue(); }, function(newVal) { $scope.value1 = newVal; }); and update that service's value when one of the buttons is clicked. Can this be done better, smaller, cleaner somehow? What's the best practice here? Cheers. Use service to

AngularJs: Binding ng-model to a list of radio buttons

泄露秘密 提交于 2019-12-01 16:44:08
Im trying to bind the selected value in a list of radio buttons to an ng-model I have: <!DOCTYPE html> <html ng-app="testApp"> <head> <script src="./bower_components/angular/angular.min.js"></script> <script src="test.js"></script> </head> <body ng-controller="testController"> <form> <div ng-repeat="option in occurrenceOptions"> <input type="radio" name="occurrence" ng-value="option" ng-model="selectedOccurrence" /><label>{{ option }}</label> </div> </form> <div>The selected value is : {{ selectedOccurrence }}</div> <!-- This works --> <input type="radio" ng-model="selected2" ng-value="'1'"> 1

AngularJS Modules and External Controllers

走远了吗. 提交于 2019-12-01 13:27:28
I have a page containing multiple containers. Each container will have its own controller but point to one factory, which handles all the logic interacting with a web service API. I would like to have a separate file for each controller BUT I want all of this inside of one module. for the life of me I cannot find how to include controllers from different files into one modules. //file 1 MyController .... //file 2 MyOtherController //file 3 MyFactory //file 4 The Module The module would be composed of MyController, MyOtherController and MyFactory defined in three separate files. Can someone

From an AngularJS controller, how do I resolve another controller function defined in the module (dynamic ng-controller)?

一个人想着一个人 提交于 2019-12-01 11:22:44
Self explanatory fiddle: http://jsfiddle.net/5FG2n/6/ I need to dynamically choose the controller to use at runtime based on its name as a string. The string will be read from a config object. In the code below I currently have InnerCtrlAsLocalVariable assigned to $scope.dynamicCtrl . How instead do I assign InnerCtrlFromModule to the property? View: <div ng-app='app' ng-controller='OuterCtrl'> <div ng-controller='dynamicCtrl'> {{message}} </div> </div> JS: var InnerCtrlAsLocalVariable = ['$scope', function($scope) { $scope.message = 'from controller as local variable - do not want' } ];

$watch not working on variable from other controller?

跟風遠走 提交于 2019-12-01 10:40:15
I have one controller which displays a checklist, and stores the selection in an array. My other controller runs an $http.get on the array from the first controller. How do I set a $watch so that whenever the array changes, a new HTTP GET request is sent? My attempt: http://plnkr.co/edit/EaCbnKrBQdEe4Nhppdfa // See plnkr for other controller + FooSelection factory + view function SimpleQueryResCtrl($scope, $http, FooSelection) { $scope.foo_list_selection = FooSelection; $scope.$watch('foo_list_selection', function (newValue, oldValue) { if (newValue !== oldValue) $http.get('/api/' + $scope.foo