简体   繁体   中英

AngularJS ui-view not receiving scope

I want to simply show a sidebar with ng-if when isCreating === true . The ng-click event is from a nmNav directive which is bound to the same controller as the state – NotesController .

But it seems that I'm not managing to pass the scope to the ui-view , though, I have stated that the controller is NotesController .

What I mean by this is that isCreating becomes true on the scope outside of ui-view when startCreating() is called.

▶︎ When setting the variables to the $rootScope everything works

What am I doing wrong? Here's all relevant code...

notesMan-app.js

angular.module('NoteMan', [
        'ui.router',
        'ngAnimate',
        'notes'
    ])
    .config(function ($stateProvider, $urlRouterProvider, $locationProvider) {
        $stateProvider
            .state('notes', {
                url: '/',
                templateUrl: 'app/components/notes/notes.tmpl.html',
                controller: 'NotesController',
                controllerAs: 'notesCtrl',
            })
            .state('note', {
                url: '/note/:title',
                templateUrl: 'app/components/notes/singleNote/notes-singleNote.tmpl.html',
                controller: 'NotesController',
                controllerAs: 'notesCtrl',
                params: {
                    title: null
                }
            });
            $urlRouterProvider.otherwise('/'); // when no routematch found redirect to /view1
            $locationProvider.html5Mode(true);
    })
    .controller('NotesController', function ($scope, $stateParams, $state, $http) {
        var vm = this;

        $scope.isEditing = false;
        $scope.isCreating = false;

        function showCreating() {
            return $scope.isCreating && !$scope.isEditing;
        }
        function startCreating() {
            $scope.isCreating = true;
            $scope.isEditing = false;

            showCreating();
        }

        //Define methods
        $scope.startCreating = startCreating;

        //Get notes from an external file
        $http.get('./app/API/notes.json').then(function (res) {
            vm.notes = res.data;
        });
    });

menu.js

angular.module('NoteMan')
    .directive('nmNav', function() {
        return {
            replace:true,
            restrict: 'E',
            templateUrl: 'app/common/menu/menu.tmpl.html',
            controller: 'NotesController as notesCtrl',
            link: function (scope, elem, attr){
                console.log(scope);
            }
        };
    });

I cant figure out call to "startCreating" in your code.

Below are my guess

a-either you have not specified the controller as "NotesController" to the container element where you are calling method "startCreating"

b-Or you have mispelled the name of controller "NotesController"or method "startCreating"

c-Or you have missed prefixing $scope to call to method "startCreating"

please check and confirm

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