angularjs - Get the argument value of a directive in a controller -
i create directive , use argument in controller:
<body ng-app="tstapp"> <navigationbar tst="hello"> </navigationbar> </body>
for create directive , controller:
navigationbar = {}; navigationbar.directives = {}; navigationbar.controllers = {}; navigationbar.directives.navigationbar = function () { return { restrict: 'e', scope: { t1: '@tst', t2: '=tst', t3: '&tst' }, templateurl: "common/navigation_bar/navigation_bar.tpl.html", controller: "navigationbarcontroller" } }; navigationbar.controllers.navigationbarcontroller = function ($scope, api) { console.log($scope); console.log($scope.t1); console.log($scope.t2); console.log($scope.t3); }; testapp.directive(navigationbar.directives); testapp.controller(navigationbar.controllers);
in console got this:
scope {$id: "003", $$childtail: null, $$childhead: null, $$prevsibling: null, $$nextsibling: null…} $$asyncqueue: array[0] $$childhead: child $$childtail: child $$destroyed: false $$isolatebindings: object $$listeners: object $$nextsibling: child $$phase: null $$prevsibling: null $$watchers: array[9] $id: "003" $parent: scope $root: scope nav: object t1: "hello" t2: undefined t3: function (locals) { this: scope __proto__: object
and:
undefined navigation_bar.js:33 undefined navigation_bar.js:34 function (locals) { return parentget(parentscope, locals); }
i understand why console.log($scope.t1); doesn't display value in scope->t1: "hello"
thank help,
julio
add information:
if replace templateurl by:
template: "scope id: {{$id }}, t1: {{ t1 }}"
i got:
scope id: 003, t1: hello
if put trace in controller this:
console.log('scope id: ' + $scope.$id + ', t1: ' + $scope.t1);
i got:
scope id: 003, t1: undefined
so scope same , shared. why can't reach t1 value in controller. should evaluate it?
the @
scoping set value isolated scope plain attribute value, string. not evaluated in way.
you can read more scoping object in angularjs directive documentation under directive definition object section.
Comments
Post a Comment