What's the angular way to get data from the partial into the controller?
NickName:John Ask DateTime:2014-05-06T08:25:37

What's the angular way to get data from the partial into the controller?

Now that Angular says using ng-init is not appropriate in most cases (source), what's the right way of passing data in, when I don't have the data I need in $scope?

I'm trying to do let future developers use a simple controller to template data sourced from a service and applied via $scope to a partial, but I need a way for them to pass an id from the template to the controller to get the correct data from the service.

Imagine Widget1 has partial widget1.html and gets its data from http://localhost/widget1.json.

Previously, I would have added Widget1 to my app by inserting the following into the correct place, and passing the widgetID using ng-init.

<div class='simple-widget' ng-include src="'widgets/widget1.html'" ng-init=" widgetID = 'widget1'"></div>

widget1.html:

<div class='simple-widget' ng-controller="SimpleWidget">
    <p>Hello {{ data.user.name }},

    <p>{{ data.message }}</p>
</div>

All of the data object will come from the DataService, as seen below.

Controller:

angular.module('myApp.controllers', [])
.controller('SimpleWidget', ['$scope','StatService',function($scope,DataService) {
    $scope.data = DataService.get({id:$scope.widgetID});
  }])

Now that ng-init is discouraged, where can I put the id so that it arrives in the controller for using in my service?

Copyright Notice:Content Author:「John」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/23484054/whats-the-angular-way-to-get-data-from-the-partial-into-the-controller

Answers
Dmitri Zaitsev 2014-05-06T03:58:35

It is true that they could be more explicit about the details of that recommendation.\n\nMy understanding of it is that in a good MVC architecture, the View should not know about the actual Model (data). The data should be supplied by Controller. Abusing ng-init breaks this pattern, that is why it is discouraged.\n\nHowever, your use case looks legitimate, in fact, I found it the easiest and cleanest way to pass variables to templates embedded with ng-include. If you'd replace ng-include by a custom directive, you would put the same variable in an attribute, which fundamentally, doesn't make any difference - both times you pass data from inside your view. So if that is all you need, I don't see any reason to write an extra directive and clutter your code, when ng-include with ng-init will do just fine.\n\nThis is, however, only good when your application is small with very few inclusions. My main HTML is very small setting all the needed variables for the templates, so it basically plays a role of configuration controller. Don't do it in a large HTMLs where you can loose track of those ng-inits.\n\nSo when you have many templates needing configuration variables, these perhaps belong to a dedicated Config service that is consumed by your Controller that sets the variables for the templates. This is what seems to be recommended.\n\nThe use of ng-repeat as mentioned is an exception, as you don't (and don't want to) have direct access to each controller of each model from your iterator. Then you have to use ng-init.",


More about “What's the angular way to get data from the partial into the controller?” related questions

What is AWS CodeArtifact? What is it and what is it useful for

I have some question to a service called AWS CodeArtifact? What is it and what is it useful for. Any links to the documentation?

Show Detail

What is AttributeSet and what is it used for?

I have read the docs, but the docs seem vague to me and I'm having a hard time understanding what it is exactly and what it's used for. Two question regarding this: What exactly is the AttributeSet

Show Detail

What is preemption / What is a preemtible kernel? What is it good for?

Explained in your own words, what is preemption and what does it mean to a (linux) kernel? What are advantages and disadvantages in having a preemptible kernel?

Show Detail

What is GLIBC? What is it used for?

I was searching for the source code of the C standard libraries. What I mean with it is, for example, how are cos, abs, printf, scanf, fopen, and all the other standard C functions written, I mean ...

Show Detail

What is CUDA like? What is it for? What are the benefits? And how to start?

I am interested in developing under some new technology and I was thinking in trying out CUDA. Now... their documentation is too technical and doesn't provide the answers I'm looking for. Also, I'd...

Show Detail

enCapsa -what is it and what is used for?

It may not be a pure programming question but I'm looking for information about enCapsa. Do you know what it is, have you ever used it? I'm reading some papers about it but I can't really see how it

Show Detail

Docker, what is it and what is the purpose

I've heard about Docker some days ago and wanted to go across. But in fact, I don't know what is the purpose of this "container"? What is a container? Can it replace a virtual machine dedicated...

Show Detail

What is applescript and what is it used for?

What is applescript and what is it used for?

Show Detail

Widget -what to do and what not

I would like to make a html/js widget that people can add to their blogs, sites etc. I have never made widgets before so I would like to hear what should be done and what not. For example, how do ...

Show Detail

What is JPQL, what does it do, and what is it used for?

What are the uses of JPQL? I'm fairly new to JPA. Doesn't the EntityManager perform the CRUD operations for you? What is JPQL used for?

Show Detail