Unable to retrieve controller properties within a view using UI router and the 'controller as' syntax

Struggling to correctly access controller object properties in my view while using ui-router for routing with nested scopes. Want to implement 'controller as' syntax but facing syntax issues.

app.js (routing setup)

(function() {

    angular

    .module('ICP_App', ['ui.router', 'satellizer', 'ngMaterial', 'ngMessages', 'xeditable'])

        .config(function($stateProvider, $urlRouterProvider, $authProvider) {

            $urlRouterProvider.otherwise('dashboard');

            $stateProvider
                .state('clients', {
                    url: '/clients',
                    templateUrl: '../partials/clients-index.html',
                    controller: 'ClientsController as ClientsCtrl'
            })
            // more routes here...

})();

ClientsController.js

(function() {

    angular.module('ICP_App')
        .controller('ClientsController', function($http) {
            $http.get('http://api.icp.sic.com/clients')
                .success(function(clients) {
                    var vm = this;
                    vm.clients = clients.data;
                    console.log(vm.clients);
                })
                .error(function(error) {
                    // handle here
                })
        });

})();

index.html

<body ng-app="ICP_App" ng-cloak>

<!-- sidebar, header etc -->

    <div ui-view></div> <!-- pull in view -->

</body>

Finally, clients-index.html partial

<div class="content">
    <div class="pane" ng-repeat="client in clients">
        {{ client.name }}
    </div>
</div>

Attempted using client in vm.clients without success.

Is there an issue with my 'controller as' syntax? Using it in ui-router but encountering errors when setting up the controller. Using 'controller as' again in the controller results in an error (

Argument ClientsController is not a
).

While console logging vm.clients displays the data, unable to access it in the view.

Thank you for any assistance.

Answer №1

Revise the ClientsController as shown below

(function() {

angular.module('ICP_App')
    .controller('ClientsController', function($http) {
        var vm=this;
        $http.get('http://api.icp.sic.com/clients')
            .success(function(clients) {                   
                vm.clients = clients.data;
                console.log(vm.clients);
            })
            .error(function(error) {
                // handle error here
            })
    }); })();

Update client-index.html as follows

<div class="content">
<div class="pane" ng-repeat="client in ClientsCtrl.clients">
    {{ client.name }}
</div>

For a deeper understanding of controller as syntax, refer to the following link

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

"Rotating the TransformControl in threejs: A step-by-step guide

When the object attached to the transform control rotates, I want the transform control itself to rotate as well. Before the rotation: https://i.sstatic.net/yjTue.png After the rotation: https://i.sstatic.net/2opuU.png As shown in the image, before th ...

Conceal certain options by utilizing multiple select criteria

I currently have a set of 3 select objects available. Colleges <select id="collegeSelect" size="8" multiple> <option value="1" >Harvard</option> <option value="2" >Stanford</option> <option value="3" >Yale& ...

Updating the React State is dependent on the presence of a useless state variable in addition to the necessary state variable being set

In my current setup, the state is structured as follows: const [items, setItems] = useState([] as CartItemType[]); const [id, setId] = useState<number | undefined>(); The id variable seems unnecessary in this context and serves no purpose in my appl ...

Avoiding repeated execution of event handlers in subviews of Backbone

Working with Backbone, I have a list container view that should house multiple section views with event handling. However, the issue is that the event is triggered as many times as there are subviews inside the container. I understand that moving the clos ...

Having trouble with parsing JSON data using jQuery?

I am currently working on retrieving a result set using postcodes with jQuery autocomplete. However, the code I have implemented seems to be displaying an empty set of rows. <html lang="en"> <head> <meta charset="utf-8> <title>Ausp ...

Is Javascript necessary for submitting a form to a PHP script?

In my current project, I am working on a page to edit information in a database. While I know how to create the form in HTML and the PHP script that runs on the server, I am facing a challenge with JavaScript. My understanding of JavaScript is limited, and ...

The Concept of Interface Segregation Principle within jQuery

Could someone provide a clear explanation of how this function operates using jQuery? Especially in reference to the response found here. It seems similar to the Single Responsibility Principle (SRP) in Object-Oriented Programming. What sets it apart? ...

Error encountered: Class not found exception in the context of JSONArray

import org.json.JSONArray; JSONArray json=new JSONArray(al); response.setContentType("application/json"); response.getWriter().print(json); } Despite having included the necessary jar in my project, I am encountering this error: SEVERE: Servle ...

Angular: displaying dates in a specific format while disregarding time zones

Is there a way to format date-time in Angular using DatePipe.format() without converting timezones, regardless of location? For instance, for various examples worldwide (ignoring time differences) I would like to obtain 07/06/2022: console.log('2022-0 ...

Vue.js is updating state, yet the view remains static and does not re-render

My experience with learning Vue.js has been great, but I keep encountering a problem where setting a data property based on state doesn't update the component when the state changes. For instance... Check out these code snippets <router-link v-i ...

Prevent the selection of a dropdown option in AngularJS once it has already

var demoApp = angular.module('myApp', []); demoApp.controller('QaController', function($scope, $http) { $scope.loopData = [{}, {}]; $scope.questions = { model: null, availableOptions: [ {id: '1& ...

Next.js: How to retrieve route parameter within getServerSideProps

I need to retrieve data from my Supabase table using the ID provided in the URL slug, for example localhost:3000/book/1, and then display information about that specific book on a page built with Next.js. Table https://i.stack.imgur.com/t5z7d.png book/[ ...

Are there any discussions underway about updating JSON standards to officially permit unquoted property names?

Technically speaking, the following JSON is considered invalid: { color: "blue", size: 14 } This is because according to the specification, property names like "color" and "size" should be enclosed in quotes, like this: { "color": "blue", "size" ...

A single feature designed to handle various elements

I currently have this HTML code repeated multiple times on my webpage: <form class="new_comment"> <input accept="image/png,image/gif,image/jpeg" id="file" class="file_comment" key=comment_id type="file" name="comment[media]"> <spa ...

Exploring the changes in rootscope with AngularJS

I am attempting to connect to $rootscope in order to establish presence on a different server when the user logs in. Below is the code for my module. angular.module('presence', [ ] ) .run(function ($rootScope) { $rootScope.$watch($rootSc ...

What is preventing my content from showing up when clicked like this?

I have created a javascript script to reveal hidden content, but for some reason it is not working when I click on it. Can anyone provide assistance? Below is the script I am using: <script src="style/jquery/jquery.js"></script> <script> ...

Create a dynamic animation page using Node.js, then seamlessly redirect users to the homepage for a smooth user

Good day everyone! I've decided to change things up with my latest query. I'm currently working on adding a loading page animation that will show for 3 seconds when visiting the '/' route, and then automatically redirect to the home pag ...

Error: The function callback.apply is not a valid function (Node.js and Mongodb)

Encountered an error when adding the line "{ upsert: true }": Error message: TypeError: callback.apply is not a function // Accessing routes that end in /users/competitorAnalysisTextData // ---------------------------------------------------- router . ...

Efficiently sending VueJS data to a separate script

I am in the process of transitioning an existing site to VueJS, but I have encountered a roadblock when it comes to finding the best method to accomplish this task. The site currently utilizes D3-Funnel (https://github.com/jakezatecky/d3-funnel) to genera ...

What sets apart a post API call from form submission using the post method?

Is it possible to call the payment gateway from Node.js using a POST API call? I understand that traditionally the payment gateway is called through form submission with the method set as post, which redirects to a new page. However, if I use a POST API ...