1

I am trying to include various html templates into a single div on the page when clicking on corresponding links (I am open to better ideas that would allow me to style links as icons)..

I am not sure why ng-include is not doing anything

Html file

    <div ng-controller="loadCtrl">

        <div>
            <ul>
                <li ng-repeat="block in blocks">

                    <a href="#" ng-model="block">
                        {{block.name}} - {{block.ctrl}}
                    </a>

                </li>
            </ul>
        </div>

        <div ng-include="block.ctrl"></div> 

    </div>

js controller

    app.controller('loadCtrl', function ($scope, $http) {

        $scope.pyctrl = base_data + 'blocks.json';
        $scope.blocks = [];

        $scope.LoadBlocks = function(data, status){
            $scope.blocks = data;
        }

        $scope.fetch = function(){
            $http.get($scope.pyctrl).success($scope.LoadBlocks,console.log("Ok"));
        }

        $scope.fetch();

    });

json file

    [
        {
            "name": "block 1",
            "ctrl": "dir1/dir2/template1.html"
        },
        {
            "name": "block 2",
            "ctrl": "dir1/dir2/template2.html"
        },
        {
            "name": "block 3",
            "ctrl": "dir1/dir2/template3.html"
        }
    ]

1 Answer 1

1

I think you wanted to keep the ng-include inside the ng-repeat loop (the li element).


Update: If you want to save a value in block when you click on a link, then instead of using ng-model="block", use ng-click="selectedBlock(block") to call a function on the scope which does this.

    $scope.block = null;
    $scope.selectBlock = function(block){
        $scope.block = block;
    }
Sign up to request clarification or add additional context in comments.

4 Comments

I don't want that. That loads all my templates automatically. I am trying to adapt angular documentation example from here docs.angularjs.org/api/ng.directive:ngInclude just by changing option list to links
How can I verify that ng-click event is firing.. because nothing is happening when I click in any shape or form?
No matter how I spin this I can't seem to get it to work. If selected block is outside the loop I can't save it. I really don't want to be a pain, but could you maybe help me with the function on the scope? I am not understanding the ng-click I guess. If I have to use the ng-repeat in a separate div (in which I am loading templates) how could I filter by selected link only?
@PersistentNewbie The problem was that selectedBlock was being created in the ng-repeat's scope and was not accessible outside. Updated the answer to use a function on the controller to do it instead.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.