在$routeChangeSuccess指令的模板中访问$scope
Accessing $scope inside template of directive on $routeChangeSuccess
目前我正在尝试用角度:)编写用于学习目的的面包屑我正在等待 $routeChangeSuccess 事件并用名称 crumb 填充一个数组。在痕迹导航的模板中使用 ng-repeat(ng-repeat)读取此数组。我还想访问范围的变量。例子:
app.directive('breadCrumb', ['$location', '$route', '$routeParams', function(location, route, routeParams) {
return {
link: function($scope, element, attrs) {
$scope.$on('$routeChangeSuccess', function(next, current) {
var loc = location.path();
if(loc.indexOf('/users') >-1 && loc!='/users')
$scope.crumb= [{href:"/users",val:"Meine Kommilitonen"},
{href:"/users/"+current.params.id,val: current.params.id}];
});
},
template: '<div ng-bind="test"></div><div style="float:left" ng-repeat="item in crumb"><a href="{{item.href}}">{{item.val}}</a></div><div style="clear:both"></div>'
}
}])
这工作得很好。但是现在我想推送一个对象,例如 {href: current.params.id, val: $scope.user.userName} 此值位于控制器中,该控制器加载路由的模板。如果我直接在模板中像{{$route.current.scope.users.userName}}一样写这个,那就好了。
现在我如何将此值推送到数组,以便将其绑定到div 并被解释为控制器的值。希望你能理解我的问题。
编辑:我已经搜索了很长时间了。我需要的是(我知道没有其他东西适合我的目的)我需要编译这样的 s.th:链接函数指令中的"{{route.current.scope.user.userName}}"。
如果我理解正确,您的控制器中有一个值,您希望它在指令中可用?
这可以通过属性 (attr) 或双向数据绑定(范围)进行访问。我将展示双向绑定示例。
JS(指令):
app.directive('breadCrumb', ['$location', '$route', '$routeParams', function(location, route, routeParams) {
return {
// add scope
scope: {
myValue: "=" // = means two way binding (you could probably use @ for one way binding)
},
link: function(scope, element, attrs) {
console.log(scope.myValue); // the value on the directive
// yourArray.push(scope.myValue);
scope.$on('$routeChangeSuccess', function(next, current) {
var loc = location.path();
if(loc.indexOf('/users') >-1 && loc!='/users')
scope.crumb= [{href:"/users",val:"Meine Kommilitonen"},
{href:"/users/"+current.params.id,val: current.params.id}];
});
},
template: '<div ng-bind="test"></div><div style="float:left" ng-repeat="item in crumb"><a href="{{item.href}}">{{item.val}}</a></div><div style="clear:both"></div>'
}
}])
JS(控制器):
$scope.someValue = {
href: "/users",
val: 1
};
.HTML:
<div bread-crumb my-value="someValue" ></div>
另一种方法是使用 attrs,不需要范围,HTML 是相同的
<div bread-crumb my-value="someValue" ></div>
.JS:
app.directive('breadCrumb', ['$location', '$route', '$routeParams', function(location, route, routeParams) {
return {
link: function(scope, element, attrs) {
console.log(attrs.myValue); // the value on the directive
// yourArray.push(attrs.myValue);
scope.$on('$routeChangeSuccess', function(next, current) {
var loc = location.path();
if(loc.indexOf('/users') >-1 && loc!='/users')
scope.crumb= [{href:"/users",val:"Meine Kommilitonen"},
{href:"/users/"+current.params.id,val: current.params.id}];
});
},
template: '<div ng-bind="test"></div><div style="float:left" ng-repeat="item in crumb"><a href="{{item.href}}">{{item.val}}</a></div><div style="clear:both"></div>'
}
}])
相关文章:
- Ng出口无法访问父$scope
- 如何从其他方法访问Angular.js$scope
- 我如何访问在 Angular JS 的 .then() 中定义的$scope数组
- 从同一控制器 Angularjs 中的另一种方法访问$scope变量
- UI-GRID:访问$scope的问题
- ng-repeat中的ng-options - 无法访问$scope中的选定项目
- AngularJS在方法之外访问$scope
- 在$routeChangeSuccess指令的模板中访问$scope
- 在AngularJS中的$http成功回调之外无法访问$scope变量值
- 在AngularJs中从视图到工厂访问$scope数据
- 在不访问$scope的情况下自动更改输入字段
- AngularJS:在$routeProvider完成请求后访问' $scope '值
- 在用函数作为模板的重复AngularJS指令中访问' $scope '
- 从AngularJS服务内部访问$scope属性
- 如何访问“$scope.search.value”
- 我如何获得登录函数能够访问$scope.names
- 无法在angular中访问$scope
- 如何在用我的HTML编写的javascript中访问scope.postresult值
- 从ng视图访问$scope时遇到问题
- AngularJS -如何从指定作用域的指令中访问$scope