简体   繁体   中英

Angular JS - {{ }} not working but data-ng-bind display the scope

I've an issue with Angular JS. I don't get why my data in the scope can't be displayed with the {{ }} but can be display if I use the directive : data-ng-bind.

If anyone has an idea :

Here's my HTML Code :

<aside  id="sidebar-left" class="sidebar-left" data-ng-controller="PortalController" data-ng-init="getAppslist()">

    <div class="sidebar-header">
        <div class="sidebar-title">
            Navigation
        </div>
        <div class="sidebar-toggle hidden-xs" data-toggle-class="sidebar-left-collapsed" data-target="html" data-fire-event="sidebar-left-toggle">
            <i class="fa fa-bars" aria-label="Toggle sidebar"></i>
        </div>
    </div>

    <div class="nano" >
        <div class="nano-content">
            <nav id="menu" class="nav-main" role="navigation">
                <ul class="nav nav-main">
                    <li class="nav-active">
                        <a href="#!/">
                            <i class="fa fa-home" aria-hidden="true"></i>
                            <span>Dashboard</span>
                        </a>
                    </li>
                    <li>
                        <a href="#!/users">
                            <i class="fa fa-users" aria-hidden="true"></i>
                            <span>Users</span>
                        </a>
                    </li>

                    <li class="nav-parent">

                        <a href="#!/apps">
                            <i class="fa fa-cubes" aria-hidden="true"></i>
                            <span>Apps</span>
                        </a>
                        <ul class="nav nav-children">
                            <li ng-repeat="app in appslist">
                                <a ng-href="#!/apps/{{app.CloudAppInstanceId}}">{{app.Name}}</a>
                            </li>
                        </ul>
                    </li>

                </ul>
            </nav>

        </div>
    </div>
</aside>

Here's my controller :

angular.module('core').controller('PortalController', ['$scope', 'Portal',
function($scope, Portal) {
    $scope.getAppslist = function() {
        Portal.getApps(function(callback) {
            $scope.appslist = callback;
            $scope.blabla = 'blabla';
        });
    };
}]);

And here's my service :

angular.module('core').factory('Portal', function($http, $cookies, $rootScope) {
// define factory object
var factory = {};



var getApps = function(callback){
    $http.get($rootScope.logrrApiAddress + '/apps', config).success(function(data, status, headers, config) {
        callback(data);
    });
}

factory.getApps = function(callback){
    return getApps(callback);
}});

Thanks in advance for your response

Martin Taz

You don't need handlebars in an ng-href. Try:

<a ng href="'#!/apps/' + app.CloudAppInstanceId">{{app.Name}}</a>

Better yet would be to turn that into a method on your controller.

<a ng href="'#!/apps/' + app.CloudAppInstanceId">{{app.Name}}</a>

Seems to me that app is not defined in the scope, what you do set in PortalController , though, is $scope.appslist .

Also, the Portal factory function must return an object instance (it currently doesn't return anything). AND the factory's method must actually return a promise, too.

angular.module('core').factory('Portal', [
    '$http', '$cookies', '$rootScope',
    function($http, $cookies, $rootScope) {

        var factory = {};  // define factory object

        factory.getApps = function(){
            // FIXME: what is "config", where is it supposed to come from?
            return $http.get($rootScope.logrrApiAddress + '/apps', config);
        };

        return factory;  // <-- you need to return something
    }
]);

angular.module('core').controller('PortalController', [
    '$scope', 'Portal',
    function($scope, Portal) {
         $scope.getAppslist = function() {
             Portal.getApps().then(function (data) {
                 $scope.apps = data;
                 $scope.blabla = 'blabla';
             });
         };
    }
]);

I changed things a bit, esp. got rid of the callback parameter - you don't need that, simply get the promise that getApps() returns and use the data once the promise is resolved.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM