Javascript AngularJS ui视图未接收范围
如果Javascript AngularJS ui视图未接收范围,javascript,angularjs,angular-ui-router,Javascript,Angularjs,Angular Ui Router,如果isCreating===true,我只想用ng显示一个侧栏。ng click事件来自nmNav指令,该指令与状态–NotesController绑定到同一控制器 但似乎我没有将作用域传递到ui视图,不过,我已经声明控制器是NotesController 我的意思是,当调用startCreating()时,isCreating在ui视图之外的范围内变为true ▶︎ 将变量设置为$rootScope时,一切正常 我做错了什么? 以下是所有相关代码 notesMan app.js angula
isCreating===true
,我只想用ng显示一个侧栏。ng click事件来自nmNav
指令,该指令与状态–NotesController
绑定到同一控制器
但似乎我没有将作用域传递到ui视图
,不过,我已经声明控制器是NotesController
我的意思是,当调用startCreating()
时,isCreating
在ui视图之外的范围内变为true
▶︎ 将变量设置为$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;
});
});
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);
}
};
});
menu.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;
});
});
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”的名称
或者您没有在$scope前面加上前缀以调用方法“startCreating”
请检查确认