javascript - Functions results in angularjs view -
i'm afraid question pretty stupid....
using {{current.name}} syntax can show name of $scope.current in view. set current when switch list view (/mythings) editing item (/mythings?id=someid).
actually redundant have information both in $location , in $scope.current. redundancy makes more complicated understand, i'd rid of it.
i replaced current current item returning function , hoped work (like in many other cases). doesn't, need write {{current().name}} everywhere, tend forget.
maybe i'm doing wrong? i'm beginner here.
is there way make work? somehow bless current gets evaluated before use?
both alternatives discussed have pros , cons:
using property (
current) easier (and more natural) reference in view, needs manually kept in sync location.using function (
current()) takes care of keeping in sync issue, less intuitive.
all things considered, value auto-syncing feature higher , go second alternative (current() function).
but, if third option combines best of both worlds ?
, in fact can :)
we can use concept of object properties, introduced ecmascript 5, , define "computed properties" our $scope (or service?).
without getting detail (see docs more details), augment scope this:
.cotroller('somectrl', function ($location, $scope) { object.defineproperty($scope, 'current', { get: function () { return { name: $location.path(); id: $location.search('id'); }; } }); now, can access $scope.current.name , $scope.current.id if current normal property of $scope (intuitiveness !) , current automatically computed based on current location (auto-sync !).
thank ecmascript 5 :)
this article provides simple , clear introduction concept.
Comments
Post a Comment