[英]AngularJS ui-view not receiving scope
我只想在isCreating === true
时显示带有ng-if的侧边栏。 ng-click事件来自nmNav
指令,该指令与状态– NotesController
绑定到同一控制器。
但是,似乎我没有设法将范围传递给ui-view
,尽管我已经声明控制器是NotesController
。
我的意思这是isCreating
的范围UI视图之外时变为真startCreating()
被调用。
▶︎ 将变量设置为$ rootScope时,一切正常
我究竟做错了什么? 这是所有相关代码...
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);
}
};
});
我无法找出在您的代码中对“ startCreating”的调用。
以下是我的猜测
a-您尚未将控制器指定为要在其中调用方法“ startCreating”的容器元素的“ NotesController”
b-或者您拼错了控制器“ NotesController”或方法“ startCreating”的名称
c-否则您错过了在$ scope之前调用方法“ startCreating”的权限
请检查并确认
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.