AngularJS ng控制器指令不接受javascript中的变量(作用域函数),也不给出任何错误
AngularJS ng-controller directive does not accept variable (scope function) from javascript, does not give any error either
我对angularJS相对陌生,我正在尝试设置一个页面,根据之前的选择调用inturn多个页面。所有的页面都有自己的控制器,所以我试图通过javascript设置控制器和视图src,并在HTML标记中使用它们。
以下是我正在做的事情:
HTML页面:
<div ng-if="sidebarName=='sidebar-device-wire'">
<div ng-controller="getSidebarCtlr">
<div ng-include src="sidebarSrc"></div>
</div>
</div>
javascript:
$scope.sidebarSrc="views/sidebars/sidebar-device.html";
$scope.sidebarCtlr="SidebarDeviceCtrl";
$scope.getSidebarCtlr = function(){return $scope.sidebarCtlr;}
但由于某种原因,这并不奏效。我可以得到HTML页面,但控制器没有被调用。有人能告诉我我做错了什么吗?
我还建议使用ngRoute
或ui.router
,因为有许多功能不容易从头开始实现(如命名视图、嵌套视图/嵌套状态或解析),而且这些模块经过了很好的测试。
不确定为什么你的控制器没有运行,但我猜在你的设置名称的控制器运行之前,已经对控制器的表达式进行了评估。所以它在编译时总是未定义的。
但是,如果你真的想实现一个非常基本的路由器,你可以在下面的演示中(或在这个小提琴中)这样做。
2015年12月21日更新
以下是我为其他SO问题写的一些路由器示例:
- 简单的ui.外部示例-jsfiddle
- 更复杂的嵌套状态示例ui.router-jsfiddle
- 使用ngRoute-jswiddle的动态链接列表
还请查看ui.router github页面以了解更多信息。
angular.module('simpleRouter', [])
.directive('simpleView', simpleViewDirective)
.provider('simpleRoutes', SimpleRoutesProvider)
.controller('MainController', MainController)
.controller('HomeController', HomeController)
.config(function(simpleRoutesProvider) {
simpleRoutesProvider.state([{
url: '/',
templateUrl: 'home.html',
controller: 'HomeController'
}, {
url: '/view1',
templateUrl: 'view1.html'
}, {
url: '/view2',
templateUrl: 'view2.html',
controller: function($scope) {
$scope.test = 'hello from controller'
}
}]);
simpleRoutesProvider.otherwise('/');
})
function HomeController($scope) {
$scope.hello = 'hello from home controller!!';
console.log('home controller started')
}
function MainController($scope) {
$scope.hello = 'Main controller test';
}
function simpleViewDirective() {
return {
restrict: 'EA',
scope: {},
template: '<div ng-include="templateUrl"></div>',
controller: function($scope, $location, $controller, simpleRoutes) {
var childControllerInst;
$scope.templateUrl = simpleRoutes.currentRoute.templateUrl || simpleRoutes.otherwise.templateUrl;
$scope.$watch(function() {
return $location.path();
}, function(newUrl) {
//console.log(newUrl)
$scope.templateUrl = simpleRoutes.changeRoute(newUrl);
childControllerInst = $controller(simpleRoutes.currentRoute.controller || function() {}, {$scope: $scope});
});
$scope.$on('$destroy', function() {
childControllerInst = undefined;
})
},
link: function(scope, element, attrs) {
}
}
}
function SimpleRoutesProvider() {
var router = {
currentRoute: {
templateUrl: ''
},
states: [],
otherwise: {},
changeRoute: function(url) {
var found = false;
angular.forEach(router.states, function(state) {
//console.log('state', state);
if (state.url == url) {
router.currentRoute = state;
found = true;
}
});
if (!found) router.currentRoute = router.otherwise;
//console.log(router.currentRoute);
return router.currentRoute.templateUrl;
}
};
this.state = function(stateObj) {
router.states = stateObj;
};
this.otherwise = function(route) {
angular.forEach(router.states, function(state) {
if (route === state.url ) {
router.otherwise = state;
}
});
//console.log(router.otherwise);
};
this.$get = function simpleRoutesFactory() {
return router;
}
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="simpleRouter" ng-controller="MainController">
<script type="text/ng-template" id="home.html">home route {{hello}}</script>
<script type="text/ng-template" id="view1.html">view1</script>
<script type="text/ng-template" id="view2.html">view2 {{test}}</script>
<div simple-view="">
</div>
<a href="#/">home</a>
<a href="#/view1">view1</a>
<a href="#/view2">view2</a>
<br/>
{{hello}}
</div>
这段代码是什么意思?$scope.getSidebarCtlr = function(){return $scope.sidebarCtlr;}
ng-directive
需要一个Controller
名称,其参数类型为string
,并且您不能传递简单的函数,您需要注册一个有效的控制器,通过controller
配方将其关联到一个模块。
https://docs.angularjs.org/guide/controller
angular.module('test', []).controller('TestCtrl', function($scope) {
$scope.greetings = "Hello World";
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<section ng-app="test">
<article ng-controller="TestCtrl">{{ greetings }}</article>
</section>
相关文章:
- Node.js v6.2.0类扩展不是函数错误
- Javascript搜索函数错误
- Chrome Javascript日期构造函数错误
- 浏览器中出现javascript初始大写函数错误
- Createjs冲突问题localToLocal不是函数错误
- Java脚本函数错误
- 更新带有Javascript函数错误的变量
- 如何将对象插入对象列表的开头,当它有 unshift 不是函数错误
- 在将 Pickadate 与 Meteor 一起使用时,未定义不是函数错误
- 运行咕噜咕噜咕噜抛出 _.object 不是函数错误
- 了解函数(错误、数据)回调
- javascript中的typeof()函数错误
- 我一直在获取angularjs服务不是函数错误(LoginService.login不是函数)
- HTML5拖拽&drop正在抛出undefined不是函数错误
- Google Maps API a.lat不是函数错误
- getItemAtIndex不是函数错误
- Javascript停止函数错误
- 对话框函数错误无效函数
- Javascript 自定义函数错误
- 因果报应单元测试'未定义'不是函数错误