[英]AngularJS ui-view not receiving scope
I want to simply show a sidebar with ng-if when isCreating === true
. 我只想在
isCreating === true
时显示带有ng-if的侧边栏。 The ng-click event is from a nmNav
directive which is bound to the same controller as the state – NotesController
. ng-click事件来自
nmNav
指令,该指令与状态– 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
. 但是,似乎我没有设法将范围传递给
ui-view
,尽管我已经声明控制器是NotesController
。
What I mean by this is that isCreating
becomes true on the scope outside of ui-view when startCreating()
is called. 我的意思这是
isCreating
的范围UI视图之外时变为真startCreating()
被调用。
▶︎ When setting the variables to the $rootScope everything works ▶︎ 将变量设置为$ rootScope时,一切正常
What am I doing wrong? 我究竟做错了什么? Here's all relevant code...
这是所有相关代码...
notesMan-app.js 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 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. 我无法找出在您的代码中对“ startCreating”的调用。
Below are my guess 以下是我的猜测
a-either you have not specified the controller as "NotesController" to the container element where you are calling method "startCreating" a-您尚未将控制器指定为要在其中调用方法“ startCreating”的容器元素的“ NotesController”
b-Or you have mispelled the name of controller "NotesController"or method "startCreating" b-或者您拼错了控制器“ NotesController”或方法“ startCreating”的名称
c-Or you have missed prefixing $scope to call to method "startCreating" c-否则您错过了在$ scope之前调用方法“ startCreating”的权限
please check and confirm 请检查并确认
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.