javascript - Difference between x.controller and .controller -
I have a main app.js file that contains the defined code below:
Var app = angular Modules ('Funapp.c', ['Funapp.controllers', 'Funapp.services',])
I'm thinking that for some JS controller files, I use the app.controller You can do this while others have to write me angular To use the module () {} etc. Controller instead? App.controller does not work for the second case.
For example. App.controller works for my posting controller.
'Strict use'; App.controller ('PostsCtrl', function ($ radius, post) {... etc}
but does not work in other JS files like factory, so I have to type it ( 'FunEp Services', []. Factor ('Post', Function ($ firebase, FIREBASE_URL) {.... etc ...
/ Code> View or Dummy also does not work in controller, postview controller below
'use strict'; //angular.module('Funapp ') -> does not currently work Is not Funapp.controller app.controller ('PostViewCtrl', funk Shan ($ scope, $ routeParams, Post) {... etc ...
What can I do to find out how to use app.controller and .controller, And if later, I should consider angular typing.Module ("", [])?
I have read the angler tutorial and understand that angular module. ("", [] ) Defines modules, whereas angular.module ("") uses that defined module. However, there are many errors that I get to the right point I am unable to solve problems and problems D how it works
It all depends on this Does the main app definition file already loaded and is within the scope of time Access the view that requires PostViewCtrl
. If it has already been loaded and available for DOM, it means that you can use the app.controller
syntax, obviously because you have already declared it And its dependence is if it is not available for DOM, then you are already doing that which is frightening from a pain and a best practices approach. For example, if you have a main page that has a script in which your app is defined, then other hosted views in that shell page can be declared by app.controller
, but If you say, server-side view, and main app definition script is not included in your master page (ASP.NET concept), then you need to re-declare your app definition, when If you want to make each controller in the server-side visits.
For example, say you have 2 pages, Home
and about
. Each of the individual files has different controller definitions: app.js
looks like this:
var app = angular Modules ("demoApp", ['demoApp.services']); Var App Services = Angular. Modules ('demoApp.services', []);
It needs to be loaded further and whenever you navigate to another page, then your second page is part of the same app, and also the services of that app Definition is required.
HomeCtrl.js
:
app.controller ("homeCtrl", function ($ scope) {$ scope.msg = "Home! ";});
amd aboutctrl.js
:
app.controller ("aboutCtrl", function ($ radius, svc1) {$ Scope.msg = "About!"; Svc1.sayHi ();});
Now Home.html
:
& lt; Divng-controller = "homectrl" & gt; & Lt; H1 & gt; {{Message}} & lt; / H1> & Lt; / Div & gt;
and about.html
:
& lt; Script src = "~ / app / Svc1.js" & gt; & Lt; / Script & gt; & Lt; Script src = "~ / app / aboutCtrl.js" & gt; & Lt; / Script & gt; & Lt; Div ng-controller = "aboutCtrl" & gt; {{Msg}} & lt; / P & gt;
and Svc1.js
looks like this:
appServices.service ("svc1", function () {return {SayHi: function () {warning ("hi");}};});
Long story short, app
and app services
variables refer to your app module, when you announce services Or the admin on your app otherwise you will need to redefine your app all the time.
Comments
Post a Comment