动态创建的指令属性在angularjs中不工作

时间:2022-04-13 08:36:58

I am uploading image and showing it on the frontend using ionic framework, I am actually working on cordova app.I want to dynamically change the directive values like

我正在上传图片并使用ionic框架在前端显示,实际上我正在处理cordova应用,我想动态地改变指令值,比如

<ion-content ng-controller="AppDetailCtrl">
        <ion-item collection-repeat="Doc in DocTypes">
            {{Doc.RequiredDocument}}
            <div>
                <img ng-show='imgURI_{{Doc.RequiredDocID}} !== undefined' width="100px" height="150px" ng-src="{{imgURI_1}}">
                <button class="button" ng-click="takePicture()">Upload Picture</button>
            </div>
        </ion-item>
    </ion-content>

It replace the values correctly in the rendered HTML but not working fine. If I write hard coded value then It's work fine. How can I achieve this dynamic behaviour.

它在呈现的HTML中正确地替换值,但不能正常工作。如果我写硬编码的值,那么它就可以工作了。我如何实现这种动态行为。

1 个解决方案

#1


2  

You should use something like this:

你应该用这样的东西:

<ion-content ng-controller="AppDetailCtrl">
    <ion-item collection-repeat="Doc in DocTypes">
        {{Doc.RequiredDocument}}
        <div>
            <img ng-show="this['imgURI_' + Doc.RequiredDocID] !== undefined" width="100px" height="150px" ng-src="{{ 'imgURI_' + Doc.RequiredDocID }}">
            <button class="button" ng-click="takePicture()">Upload Picture</button>
        </div>
    </ion-item>
</ion-content>

this['imgURI_' + Doc.RequiredDocID] might look a little weird but in fact this is just normal for javascript bracket notation, in your case, a way to refer property of the object (this - is a scope object) by variable name ('imgURI_' + Doc.RequiredDocID).

这(imgURI_ + Doc。RequiredDocID看起来有点奇怪,但实际上对于javascript括号符号来说,这是很正常的,在您的例子中,这是一种通过变量名('imgURI_' + Doc.RequiredDocID)引用对象属性的方法。

#1


2  

You should use something like this:

你应该用这样的东西:

<ion-content ng-controller="AppDetailCtrl">
    <ion-item collection-repeat="Doc in DocTypes">
        {{Doc.RequiredDocument}}
        <div>
            <img ng-show="this['imgURI_' + Doc.RequiredDocID] !== undefined" width="100px" height="150px" ng-src="{{ 'imgURI_' + Doc.RequiredDocID }}">
            <button class="button" ng-click="takePicture()">Upload Picture</button>
        </div>
    </ion-item>
</ion-content>

this['imgURI_' + Doc.RequiredDocID] might look a little weird but in fact this is just normal for javascript bracket notation, in your case, a way to refer property of the object (this - is a scope object) by variable name ('imgURI_' + Doc.RequiredDocID).

这(imgURI_ + Doc。RequiredDocID看起来有点奇怪,但实际上对于javascript括号符号来说,这是很正常的,在您的例子中,这是一种通过变量名('imgURI_' + Doc.RequiredDocID)引用对象属性的方法。